在 JavaScript 中发送 HTTP 请求的方法有多种,常见的方法包括使用 XMLHttpRequest 对象和使用 fetch API。
XMLHttpRequest 示例代码:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data.json', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send();
文章介绍了在JavaScript中发起HTTP请求的两种常见方式:使用XMLHttpRequest对象和fetchAPI。示例代码展示了如何用XMLHttpRequest进行GET请求,并处理响应数据。
912

被折叠的 条评论
为什么被折叠?



