fetch API
by 개발하는지호Ajax 프로그래밍 방식에서 주로 사용하던 API는 XMLHttpRequest였는데, 보다 간소화되어 사용이 조금 더 편리해진 API
무언가를 가져오다, 불러오다를 뜻하는 Fetch로 네이밍된 API이다.
기본 사용법
활용 예시
const detectLanguage = async (text) => {
let sourceLanguage;
const url = '/detectLangs';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query: text }),
};
await fetch(url, options)
.then(response => response.json())
.then(data => {
sourceLanguage = data.langCode;
sourceSelect.value = sourceLanguage;
})
.catch(error => console.error(error));
return sourceLanguage;
}
'개발 지식, 스크립트 > JavaScript, TS' 카테고리의 다른 글
Promise 객체의 상태 (0) | 2023.12.10 |
---|---|
Promise (0) | 2023.12.10 |
[Express.js] express.static() 정적 파일 서비스 (1) | 2023.12.03 |
동기와 비동기 (0) | 2023.12.01 |
Web API (좀더 정리 필요) (0) | 2023.11.29 |
블로그의 정보
DevSecOps
개발하는지호