js怎么发送一个请求
要发送一个请求,需要使用XMLHttpRequest对象或fetch API。
使用XMLHttpRequest对象:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api/data');
xhr.onload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.log('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
使用fetch API:
fetch('https://example.com/api/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
原文地址: https://www.cveoy.top/t/topic/bgaM 著作权归作者所有。请勿转载和采集!