您可以使用 XMLHttpRequest
或 fetch
API 在 JavaScript 中發(fā)出 HTTP 請求。
使用 XMLHttpRequest:
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api/data');
xhr.onload = () => {
if (xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send();
使用 fetch:文章來源:http://www.zghlxwxcb.cn/news/detail-728347.html
fetch('https://example.com/api/data')
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
請注意,這些示例代碼都是 GET 請求,您可以根據(jù)需要更改為 POST、PUT 等請求方法。文章來源地址http://www.zghlxwxcb.cn/news/detail-728347.html
到了這里,關(guān)于如何在 Javascript 中發(fā)出 HTTP 請求?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!