【发布时间】:2014-02-21 05:40:23
【问题描述】:
我有一小段代码,我在 Firefox Scratchpad 中进行了测试,以获取包含所有当前可用的 Ubuntu Click Apps 的 json 文件:
function getHttp(url, callback) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
callback(req.responseText);
}
};
req.open("GET", url, true);
req.send();
}
getHttp("http://search.apps.ubuntu.com/api/v1/search?q=", function(response) {
console.log(response);
});
直接从暂存器运行时,它会正确获取所需的 json。但是,当我将它放在一个小 html 应用程序的 javascript 文件中时,我正在编写它只返回一个空字符串响应。
(页面托管在 xampp 安装上)
我已经对 CORS 的主题进行了很多研究,但它似乎对我没有帮助,因为必须在服务器上设置“Access-Control-Allow-Origin:”属性,对吧?
我还尝试将 'withCredentials' 标志设置为 true...但它不会改变任何东西。
那么为什么它可以与暂存器一起使用,但当我从 http://localhost/project/index.html 访问它时却不行?
这是我得到的响应标头:
"Date: Wed, 29 Jan 2014 12:04:52 GMT
Server: gunicorn/0.15.0
Content-Type: application/json
Expires: Wed, 29 Jan 2014 12:05:52 GMT
X-Bzr-Revision-Number: 68
Cache-Control: max-age=60, private
Content-Length: 29207
X-Cache: MISS from localhost
X-Cache-Lookup: MISS from localhost:3128
Via: 1.0 localhost (squid/3.1.19)
Strict-Transport-Security: max-age=2592000"
非常感谢任何提示...我已经挖掘并尝试了几个小时,但我真的不知道从这里去哪里
【问题讨论】:
标签: javascript json xmlhttprequest cors