ajax
var xhr = new XMLHttpRequest(); // 创建
xhr.open('GET', url, true);
xhr.onreadystatechange = function () { // onreadystatechange 不是检测方法,而是状态改变后更新的状态
if (xhr.status == 404) {
if (xhr.readyState == 4) {
return true;
// var data = JSON.parse(xhr.responseText); // responseText 是返回的文本或对象
}
} else {
// 如果不是正常返回
// console.log("数据返回失败!状态码" + xhr.status + "状态信息:" + xhr.statusText)
// xhr.statusText是浏览器的错误信息,因为跨浏览器的时候,可能不太一致,不建议直接使用它
return false;
}
}
// xhr.setRequestHeader("Content-Type","application/www-x-form-urlencoded");
xhr.send(null); // 向服务器发送请求 //post参数 get 为空