1 首先引入Ajax的封装包(ajax.js需要自己写)
<script type="text/javascript" src="Ajax.js"></script>
/**
* Ajax 封装包
* Created by LL on 2019-02-27.
*/
//url:读取的文件路径
// fnSucc:读取成功操作
// fnFaild:读取失败返回状态码
function Ajax(url,fnSucc,fnFaild) {
//1 创建Ajax对象
//在ie6下没有XMLHttpRequest这个属性,因此XMLHttpRequest属于未定义,需要用window.XMLHttpRequest
if (window.XMLHttpRequest) {
var oAjax = new XMLHttpRequest();
} else {
//IE6
var oAjax = new ActiveXObject("Microsoft.XMLHTTP");
}
//2 连接服务器
//open(方法,文件名,异步传输)
oAjax.open('GET', url, true);
//3 发送请求
oAjax.send();
//4 从服务器接收返回
oAjax.onreadystatechange = function () {
/*readyState属性:请求状态(表示浏览器和服务器,进行到哪一步了)
*0 (未初始化)还没有调用open()方法
*1 (载入)已调用send()方法,正在发送请求
*2