Javascript에서 API를 호출하는 4가지 방법
5695 단어 ajaxjavascriptcodenewbiehtml
🔎 XML HTTP 요청
var request = new XMLHttpRequest();
request.open('GET', 'https://api.github.com/users/anuradha9712');
request.send();
request.onload = ()=>{
console.log(JSON.parse(request.response));
}
🔎 가져오기
.then()은 Promise가 완료되면 무엇을 해야 하는지 프로그램에 알려줍니다.
fetch('https://api.github.com/users/anuradha9712')
.then(response =>{
return response.json();
}).then(data =>{
console.log(data);
})
🔎 악시오스
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
axios.get('https://api.github.com/users/anuradha9712')
.then(response =>{
console.log(response.data)
})
🔎 jQuery AJAX
$.ajax()
메서드를 사용하여 요청합니다.<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
$(document).ready(function(){
$.ajax({
url: "https://api.github.com/users/anuradha9712",
type: "GET",
success: function(result){
console.log(result);
}
})
})
마무리!!
시간 내 주셔서 감사합니다!! 함께 배우고 성장하기 위해 연결합시다.
![](https://s1.md5.ltd/image/89bb5cc45035df160aaf8251c9a8abdc.png)
Reference
이 문제에 관하여(Javascript에서 API를 호출하는 4가지 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/anuradha9712/four-ways-to-make-an-api-calls-in-javascript-ilc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)