Empty responseText from XMLHttpRequest(来自 XMLHttpRequest 的空 responseText)
问题描述
我写了一个 XMLHttpRequest,它运行良好但返回一个空的 responseText.
I have written an XMLHttpRequest which runs fine but returns an empty responseText.
javascript如下:
The javascript is as follows:
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);
}
}
}
代码运行良好.我可以走过去,我得到了 readyState == 4 和一个状态 == 200 但 responseText 总是空白.
The code runs fine. I can walk through and I get the readyState == 4 and a status == 200 but the responseText is always blank.
我收到错误调度的日志错误(在 Safari 调试中):getProperties,我似乎无法找到参考.
I am getting a log error (in Safari debug) of Error dispatching: getProperties which I cannot seem to find reference to.
我已经在本地和远程服务器上的 Safari 和 Firefox 中运行了代码.
I have run the code in Safari and Firefox both locally and on a remote server.
将 URL 放入浏览器时将返回字符串并给出状态码 200.
The URL when put into a browser will return the string and give a status code of 200.
我在一个运行良好的 Mac Widget 中为相同的 URL 编写了类似的代码,但在浏览器中的相同代码永远不会返回结果.
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.
推荐答案
http://api.xxx.com/
是否属于您的域?如果没有,您将被同源政策阻止.
Is http://api.xxx.com/
part of your domain? If not, you are being blocked by the same origin policy.
您可能需要查看以下 Stack Overflow 帖子,了解一些可能的解决方法:
You may want to check out the following Stack Overflow post for a few possible workarounds:
- 规避同源策略的方法
这篇关于来自 XMLHttpRequest 的空 responseText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自 XMLHttpRequest 的空 responseText


基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01