function getXHR() {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject('Msxml2.XMLHTTP');//MSXML3
} catch (e) {
try {
xhr = new ActiveXObject('Microsoft.XMLHTTP')
}
catch (e) {
alert('不支持');
}
}
}
return xhr;
}
var xhr = getXHR();
xhr.open('GET', url, true);//true 是否异步
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = xhr.responseText;
console.log(data);
}
}
}
xhr.onerror = function () {
console.log('error');
}
xhr.send();//发送请求
一个原生 JS 请求
最新推荐文章于 2024-04-11 15:18:10 发布