最简单的封装的AJAX:

function myajax(url,onsuccess,fail){
    //确定是否支持xhr
    var xhr = new XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
    //准备发送post异步请求
    xhr.open('POST', url, true);
    //监听并获得返回结果
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) { //4 表示执行完
            if (xhr.status == 200) { //200 表示执行成功
                onsuccess(xhr.responseText);
            } else {
                if (fail) { //有传fail才传出失败信息
                    fail(xhr.status);
                }
            }
        }
    }
    //发送请求
    xhr.send();
}
myajax.js

相关文章: