js-ajax-04

function ajax(url,fnSucc,fnError) {
    if (window.XMLHttpRequest) {
        var httpRequest = new XMLHttpRequest();
    } else {
        var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //2 连接服务器
    httpRequest.open('GET', url, true);
    //3 发送请求
    httpRequest.send();
    //4 接受结果
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            if (httpRequest.status === 200) {
                fnSucc && fnSucc(httpRequest.responseText);
            } else {
                fnError && fnError();
            }
        }
    }
}

 

posted @ 2017-11-24 12:16  Jinsuo  阅读(84)  评论(0编辑  收藏  举报