ajax监听事件

1.创建ajax的核心对象
var xhr = null;
if (window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
console.log('就绪状态' + xhr.readyState);
console.log('状态码' + xhr.status);
console.log('响应文本' + xhr.responseText);
console.log("\n")

// 2.绑定监听事件
xhr.onreadystatechange = function(){
console.log('就绪状态' + xhr.readyState);
console.log('状态码' + xhr.status);
console.log('响应文本' + xhr.responseText);
console.log('\n')
}
// 3.创建请求的消息,连接服务器 readyState 0 => 1
xhr.open('GET','02.php',true);
// 1.请求的方式 POST/GET
// 2.请求的地址
// 3.[同步||异步] true表示异步,默认

// 4.发送请求
xhr.send(null)
// get请求的参数一般放在url后面,
// post请求参数就写在send中

// status状态码
// 1xx 信息类错误
// 2xx 成功 200
// 3xx 重定向 304
// 4xx 客户端错误 404未找到
// 5xx 服务端错误 506

猜你喜欢

转载自www.cnblogs.com/6909ye/p/9766614.html