Javascript 来自 XMLHttpRequest 的空响应文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1941340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:59:46  来源:igfitidea点击:

Empty responseText from XMLHttpRequest

javascriptajaxxmlhttprequest

提问by PurplePilot

I have written an XMLHttpRequest which runs fine but returns an empty responseText.

我编写了一个运行良好但返回空响应文本的 XMLHttpRequest。

The javascript is as follows:

javascript如下:

  var anUrl = "http://api.xxx.com/rates/csv/rates.txt";
  var myRequest = new XMLHttpRequest();

  callAjax(anUrl);

  function callAjax(url) {
     myRequest.open("GET", url, true);
     myRequest.onreadystatechange = responseAjax;
                 myRequest.setRequestHeader("Cache-Control", "no-cache");
     myRequest.send(null);
  }

  function responseAjax() {
     if(myRequest.readyState == 4) {
        if(myRequest.status == 200) {
            result = myRequest.responseText;
            alert(result);
            alert("we made it");
        } else {
            alert( " An error has occurred: " + myRequest.statusText);
        }
     }
  }

The code runs fine. I can walk through and I get the readyState == 4 and a status == 200 but the responseText is always blank.

代码运行良好。我可以走过去,我得到 readyState == 4 和 status == 200 但 responseText 始终为空。

I am getting a log error (in Safari debug) of Error dispatching: getProperties which I cannot seem to find reference to.

我收到 Error dispatching: getProperties 的日志错误(在 Safari 调试中),我似乎找不到参考。

I have run the code in Safari and Firefox both locally and on a remote server.

我已经在本地和远程服务器上在 Safari 和 Firefox 中运行了代码。

The URL when put into a browser will return the string and give a status code of 200.

放入浏览器的 URL 将返回字符串并给出 200 的状态代码。

I wrote similar code to the same URL in a Mac Widget which runs fine, but the same code in a browser never returns a result.

我在运行良好的 Mac Widget 中为相同的 URL 编写了类似的代码,但浏览器中的相同代码永远不会返回结果。

采纳答案by Daniel Vassallo

Is http://api.xxx.com/part of your domain? If not, you are being blocked by the same origin policy.

http://api.xxx.com/您域的一部分吗?如果没有,您将被同源策略阻止。

You may want to check out the following Stack Overflow post for a few possible workarounds:

您可能需要查看以下 Stack Overflow 帖子以了解一些可能的解决方法:

回答by Ivan David

PROBLEM RESOLVED

问题已解决

In my case the problem was that I do the ajax call (with $.ajax, $.get or $.getJSON methods from jQuery) with full pathin the url param:

在我的情况下,问题是我在 url 参数中使用完整路径执行 ajax 调用(使用 $.ajax、$.get 或 $.getJSON 方法来自 jQuery):

url: "http://mydomain.com/site/cgi-bin/serverApp.php"

网址:“ http://mydomain.com/site/cgi-bin/serverApp.php

But the correct way is to pass the value of url as:

但正确的方法是将 url 的值传递为:

url: "site/cgi-bin/serverApp.php"

网址:“站点/cgi-bin/serverApp.php”

Some browser don't conflict and make no distiction between one text or another, but in Firefox 3.6 for Mac OS take this full pathas "cross site scripting"... another thing, in the same browser there is a distinction between:

一些浏览器不冲突,也不区分一个文本或另一个文本,但在 Mac OS 的 Firefox 3.6 中将此完整路径作为“跨站点脚本”......另一件事,在同一浏览器中,两者之间存在区别:

http://mydomain.com/site/index.html

http://mydomain.com/site/index.html

And put

并放

http://www.mydomain.com/site/index.html

http://www.mydomain.com/site/index.html

In fact it is the correct point view, but most implementations make no distinction, so the solution was to remove all the text that specify the full pathto the script in the methods that do the ajax request AND.... remove any BASEtag in the index.html file

事实上,这是正确的观点,但大多数实现没有区别,所以解决方案是删除所有指定脚本完整路径的文本,这些文本在执行 ajax 请求的方法中指定了脚本的完整路径AND.... 删除任何BASE标记在 index.html 文件中

base href="http://mydomain.com/" <--- bad idea, remove it!

base href="http://mydomain.com/" <--- 坏主意,删除它!

If you don't remove it, this version of browser for this system may take your ajax request like if it is a cross site request!

如果你不删除它,这个系统的这个版本的浏览器可能会把你的ajax请求当作跨站请求!

I have the same problem but only on the Mac OS machine. The problem is that Firefox treat the ajax response as an "cross site" call, in any other machine/browser it works fine. I didn't found any help about this (I think that is a firefox implementation issue), but I'm going to prove the next code at the server side:

header('Content-type: application/json');

to ensure that browser get the data as "json data" ...

我有同样的问题,但只在 Mac OS 机器上。问题是 Firefox 将 ajax 响应视为“跨站点”调用,在任何其他机器/浏览器中它都可以正常工作。我没有找到任何有关此的帮助(我认为这是一个 Firefox 实现问题),但我将在服务器端证明下一个代码:

header('Content-type: application/json');

确保浏览器将数据作为“json 数据”...

回答by Jacob Relkin

The browser is preventing you from cross-site scripting.

浏览器阻止您进行跨站点脚本编写。

If the url is outside of your domain, then you need to do this on the server side or move it into your domain.

如果 url 在您的域之外,那么您需要在服务器端执行此操作或将其移动到您的域中。

回答by swl1020

This might not be the best way to do it. But it somehow worked for me, so i'm going to run with it.

这可能不是最好的方法。但它以某种方式对我有用,所以我要运行它。

In my php function that returns the data, one line before the return line, I add an echo statement, echoing the data I want to send.

在我的返回数据的 php 函数中,在返回行前一行,我添加了一个 echo 语句,回显我要发送的数据。

Now sure why it worked, but it did.

现在确定它为什么起作用了,但它确实起作用了。

回答by wkm

Had a similar problem to yours. What we had to do is use the document.domain solution found here:

遇到了和你类似的问题。我们必须做的是使用这里找到的 document.domain 解决方案:

Ways to circumvent the same-origin policy

规避同源策略的方法

We also needed to change thins on the web service side. Used the "Access-Control-Allow-Origin" header found here:

我们还需要更改 Web 服务端的细化。使用此处找到的“Access-Control-Allow-Origin”标头:

https://developer.mozilla.org/En/HTTP_access_control

https://developer.mozilla.org/En/HTTP_access_control