Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLHttpRequest POST返回空白响应文本_Javascript_Ajax_Post - Fatal编程技术网

Javascript XMLHttpRequest POST返回空白响应文本

Javascript XMLHttpRequest POST返回空白响应文本,javascript,ajax,post,Javascript,Ajax,Post,我试图在Javascript中通过AJAX发布HTTP帖子(不使用jquery) 服务器端正在收到帖子并发送响应。我知道有响应击中了客户端,因为我使用http://code.google.com/p/chrome-rest-client/,它用服务器按预期发送的JSON进行响应 我将整个XMLHttpRequest对象转储到控制台,但仍然看不到缺少什么: statusText status 0 response responseType responseXML null resp

我试图在Javascript中通过AJAX发布HTTP帖子(不使用jquery)

服务器端正在收到帖子并发送响应。我知道有响应击中了客户端,因为我使用
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请求有什么问题吗?

我找到了答案

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

编辑:为每个请求添加注释

您可以尝试改用jQuery ajax。使用jQueryAjax,您不必担心xmlHttp对象。浏览器开发工具(F12)可以帮助跟踪网络请求,并确定响应中是否包含文本。没有实际内容的200-OK响应仍然有效。状态0和RS1应该是第一条线索,但您需要将true更改为false或使用asyn等待响应responseText@RajaAnbazhagan每个请求添加评论
  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));
  };