ajax基础

function ajax(url, fn) {
       //创建对象
       // IE早期版本是new ActiveXObject() 高级版本已不支持
        var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {// 状态发生变化时,函数被回调
            // 监听readystate
           if (xhr.readyState === 4) {
                
                if (xhr.status === 200) {
                    // 成功,通过responseText拿到响应的文本:
                     console.log(xhr.responseText);
                } else {
                    // 失败,根据响应码判断失败原因:
                    console.log(xhr.status);
                }
           }else {
                // HTTP请求还在继续...
            }
        }
       //打开链接
xhr.open('GET', url, true);
//发送请求
xhr.send(null);
}

ajax基于XMLHttpRequest

关键方法:

XMLHttpRequest
open
send
readystatechange

关键属性:

readyState
status
responseText(还有responseXML )
posted @ 2017-07-25 21:02  永醉雨辰  阅读(106)  评论(0编辑  收藏  举报