1.初识原生AJAX
var Myxhr;
//------------创建AJAX对象---------------------
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
Myxhr = new XMLHttpRequest();
}
else {
// IE6, IE5 浏览器执行代码
Myxhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//-------------构造请求------------------
Myxhr.open("GET", "/test_get", true);//初始化请求 请求方式,url,同步false异步true 默认为异步
Myxhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");//设置请求头,必须在请求初始化之后
Myxhr.send();//发送请求
//-------------请求回调--------------------
Myxhr.onreadystatechange = function () {//请求状态变化事件 同步请求无效
if (Myxhr.readyState == 4) {//只有
console.log(Myxhr.responseText);
}
};
/*
**XMLHttpRequest常用属性:
**readyState请求状态 0,1,2,3,4
**status:200,404'未找到页面'
**responseText:获得字符串形式的响应数据。
**responseXML:获得 XML 形式的响应数据。
*/
待续。。。。。。。。。。。先发出来促使自己去完成题目的要求
参考:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/onreadystatechange
https://www.runoob.com/ajax/ajax-xmlhttprequest-response.html