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