2014-09-30 178 views
0

我試圖使通過AJAX一個HTTP POST在Javascript(不使用jQuery)XMLHttpRequest的POST返回空responseText的

ajax_post = function(aData) { 
    var k, v, xmlhttp; 
    xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open("POST", aData.path, true); 
    xmlhttp.setRequestHeader('Content-Type', 'application/json'); 
    xmlhttp.send(JSON.stringify(aData)); 
    return xmlhttp.responseText; 
}; 

服務器端獲得這個職位,併發送一個響應。我知道一個響應正在打到客戶端,因爲我用http://code.google.com/p/chrome-rest-client/測試了服務器,它用JSON響應服務器按預期發送。

我轉儲整個XMLHttpRequest對象到控制檯,但仍然沒有看到我錯過了什麼:

statusText 
status 0 
response 
responseType 
responseXML null 
responseText 
upload 
XMLHttpRequestUpload {ontimeout: null, onprogress: null, onloadstart: null, onloadend: null, onload: null…} 
withCredentials false 
readyState 1 
timeout 0 
onreadystatechange null 
ontimeout null 
onprogress null 
onloadstart null 
onloadend null 
onload null 
onerror null 
onabort null 
open function open() { [native code] } 
setRequestHeader function setRequestHeader() { [native code] } 
send function send() { [native code] } 
abort function abort() { [native code] } 
getAllResponseHeaders function getAllResponseHeaders() { [native code] } 
getResponseHeader function getResponseHeader() { [native code] } 
overrideMimeType function overrideMimeType() { [native code] } 
UNSENT 0 
OPENED 1 
HEADERS_RECEIVED 2 
LOADING 3 
DONE 4 
addEventListener function addEventListener() { [native code] } 
removeEventListener function removeEventListener() { [native code] } 
dispatchEvent function dispatchEvent() { [native code] } 

什麼是錯我的客戶端POST請求?

+0

您可以嘗試使用jQuery AJAX來代替。使用jQuery ajax,您不必擔心xmlHttp對象。 – Ragnar 2014-09-30 21:29:57

+0

瀏覽器開發人員工具(F12)可以幫助跟蹤網絡請求,並確定響應中是否實際存在文本。沒有實際內容的200 OK響應仍然有效。 – Katana314 2014-09-30 21:32:37

+1

狀態零和RS1應該是第一個線索,但是您需要將其更改爲false或使用asyn來等待響應文本 – dandavis 2014-09-30 21:32:50

回答

0

我想通了

ajax_post = function(aData, aCB) { 
    var xmlhttp; 
    xmlhttp = void 0; 

    // Fallback for IE5/6 
    if (window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } else { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    // Send Request to Server 
    xmlhttp.open("POST", aData.path, true); 

    // Set Header Information specifying what type of data you are sending 
    xmlhttp.setRequestHeader('Content-Type', 'application/json'); 

    // Callback that waits untill request is finished and responce is ready 
    xmlhttp.onreadystatechange = (function(_this) { 
     return function() { 
     // readyState 
     // - 0: request not initialized 
     // - 1: server connection established 
     // - 2: request received 
     // - 3: processing request 
     // - 4: request finished and response is ready 

     // status 
     // - 200: OK 
     if ((aCB != null) && xmlhttp.readyState === 4 && xmlhttp.status === 200) { 
      aCB(xmlhttp.responseText); 
     } 
     // TODO check for xmlhttp.status !== 200 Because error handeling should be done 
     }; 
    })(this); 

    // Sends Request Request Payload 
    xmlhttp.send(JSON.stringify(aData)); 
    }; 

編輯:添加註釋每個請求

+0

如果您可以解釋它,IT將會有所幫助...... – 2016-01-22 23:14:53

+0

@RajaAnbazhagan爲每個請求添加了註釋 – CaffeineAddiction 2016-01-25 16:12:18

+0

您。現在,這是一個人們想要參考學習的答案... – 2016-01-25 17:37:18