xhr简介
1、简介
XMLHttpRequest
(XHR)对象用于与服务器交互。通过XMLHttpRequest
可以在不刷新页面的情况下请求特定 URL,获取数据。这允许网页在不影响用户操作的情况下,更新页面的局部内容。XMLHttpRequest
在 AJAX 编程中被大量使用。
XMLHttpRequest
是一组 API 函数集,可被JavaScript
、JScript
、VBScript
以及其它 web浏览器内嵌的脚本语言调用,通过 HTTP 在浏览器和 web服务器之间收发 XML 或其它数据。
2、示例
2.1、发送请求
javascript
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.baidu.com");
xhr.onload = () => {
console.log(xhr.responseText);
}
xhr.send();
2.2、封装axios
ts
export const axios = {
get<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = () => {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(xhr.statusText);
}
};
xhr.send();
});
}
}
使用
ts
axios.get("https://www.baidu.com")