js 发送http请求


// 1、创建 XHR对象(IE6- 为ActiveX对象)
// 2、连接及发送请求
// 3、回调处理
function createXMLHttpRequest() {
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
        if (xhr.overrideMimeType)
            xhr.overrideMimeType('text/xml');
    } else if (window.ActiveXObject) {
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    return xhr;
}
var xhr = createXMLHttpRequest();
if (xhr != null) {
    xhr.open("get"urltrue);
    xhr.send(null);
    xhr.onreadystatechange = function () {
        // readyState 五种状态
        // 0 - (未初始化)调用了open()方法,未调用send()方法
        // 1 - (载入)send()方法,正在发送请求
        // 2 - (载入完成)send()方法执行完成,已经接收到全部响应内容
        // 3 - (交互)正在解析响应内容
        // 4 - (完成)响应内容解析完成
        if (xhr.readyState == 4) {
            // status:http状态码
            if (xhr.status >= 200 && xhr.status < 300) {
                
            } else {
                
            }
        }
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM