【问题标题】:XMLHttpRequest POST returning blank responseTextXMLHttpRequest POST 返回空白 responseText
【发布时间】:2014-09-30 21:15:24
【问题描述】:

我正在尝试通过 Javascript 中的 AJAX 进行 HTTP POST(不使用 jquery)

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;
};

服务器端正在获取 POST 并正在发送响应。我知道一个响应正在到达客户端,因为我使用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 请求有什么问题?

【问题讨论】:

  • 您可以尝试使用 jQuery ajax。使用 jQuery ajax,您不必担心 xmlHttp 对象。
  • 浏览器开发工具 (F12) 可以帮助跟踪网络请求,并确定响应中是否真的有文本。没有实际内容的 200-OK 响应仍然有效。
  • 状态0和RS1应该是第一条线索,但是你需要将true改为false或者使用asyn等待responseText

标签: javascript ajax post


【解决方案1】:

我想通了

  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));
  };

编辑:为每个请求添加 cmets

【讨论】:

  • @RajaAnbazhagan 为每个请求添加了 cmets
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多