【发布时间】:2014-11-13 10:49:27
【问题描述】:
我目前正在使用the cloudapp API 进行一个项目,并且我正在使用jquery。 这是我的代码:
$.ajax({
headers: { "Accept": "application/json"},
type: 'GET',
url: 'http://cl.ly/2wr4',
crossDomain: true,
success: function(data, textStatus, request){
console.log(data);
}
});
当我运行它时,我得到 200 OK 响应和 Firefox 中的这个错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://cl.ly/2wr4. This can be fixed by moving the resource to the same domain or enabling CORS.
Google Chrome 中的这个错误:
XMLHttpRequest cannot load http://cl.ly/2wr4. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
并且没有任何内容记录到控制台。 请问这个错误怎么解决?
谢谢。
【问题讨论】:
-
看看这个问题:stackoverflow.com/questions/5750696/… 你需要添加 Access-Control-Allow-Origin 标头。
-
最好使用像 PHP 的 cURL 这样的服务器端库。在这里可以对服务器端页面进行 ajax 调用,该页面具有这样的库,可以进行调用并获取结果
标签: javascript jquery ajax cors