Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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总是得到一个空响应,而通过浏览器直接ping相同的url会得到正确的响应_Javascript_Ruby On Rails_Xmlhttprequest - Fatal编程技术网

Javascript XMLHttpRequest总是得到一个空响应,而通过浏览器直接ping相同的url会得到正确的响应

Javascript XMLHttpRequest总是得到一个空响应,而通过浏览器直接ping相同的url会得到正确的响应,javascript,ruby-on-rails,xmlhttprequest,Javascript,Ruby On Rails,Xmlhttprequest,以下是我的javascript代码片段: var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4) { if (this.status == 200) { ... } } } var url = 'http://localhost:3000/questio

以下是我的javascript代码片段:

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if (this.readyState == 4) {
        if (this.status == 200) {
          ...
        } 
      }
    }
    var url = 'http://localhost:3000/questions/1';
    xhr.open('GET', url, true);
    xhr.send();
this.status始终为0,this.responseText始终为“”

如果我ping url'http://localhost:3000/questions/1'我直接从浏览器中获取正确的html


仅供参考,我正在ping我机器上运行的rails服务器。

我会尝试在您的代码中用
xhr
替换
this
。函数声明将通过闭包保留
xhr
变量。我不确定当函数被回调时,这个将是什么。

您从哪个域调用它?是
http://localhost:3000
太多?确保主机:端口与