jquery的CORS错误 [英] CORS error with jquery

查看:918
本文介绍了jquery的CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 cloudapp API 开展一个项目我使用jquery。
这是我的代码:

  $ .ajax({
header:{Accept: application / json},
type:'GET',
url:'http://cl.ly/2wr4',
crossDomain:true,
success:function data,textStatus,request){
console.log(data);
}
});

当我运行这个我得到200 OK响应和Firefox中的此错误:

 跨原始请求被阻止:同源策略不允许在http://cl.ly/2wr4读取远程资源。这可以通过将资源移动到相同的域或启用CORS来修复。 

并在Google Chrome中出现此错误:

  XMLHttpRequest无法加载http://cl.ly/2wr4。在所请求的资源上没有Access-Control-Allow-Origin头。因此,不允许原始null访问。 

,并且没有任何内容记录到控制台。
请如何解决这个错误?



谢谢。

解决方案

CORS(跨源资源共享)是一种HTML5功能,允许一个网站访问另一个网站的资源,域名。



对于CORS的W3C规范实际上提供了一些简单的响应头例子,例如键头, Access-



如果您正在查找特定信息,则必须使用这些标头来控制允许原始关于如何在各种常见的Web服务器上设置CORS,请查看启用CORS共享网站。 http://enable-cors.org/



根据上面给出的URL,我不认为你对该服务器有控制权。



即使您已启用crossDomain:true,服务器应返回所需的标头,例如:



这是一个有效的服务器响应;

 存取权限-Control-Allow-Origin:http:// xyzcom 
Access-Control-Allow-Credentials:true
Access-Control-Expose-Headers:FooBar
Content-Type:text / html; charset = utf-8

你可以尝试的一件事是:

  $。ajax({
headers:{Accept:application / json},
type:'GET',
url:'http://cl.ly/2wr4',
crossDomain:true,
beforeSend:function(xhr){
xhr.withCredentials = true;
},
success:function(data,textStatus,request){
console.log(data);
}
}

否则,您需要与API提供商联系。


I'm currently working on a project using the cloudapp API and I'm using jquery. Here is my code:

 $.ajax({
            headers: { "Accept": "application/json"},
            type: 'GET',
            url: 'http://cl.ly/2wr4',
            crossDomain: true,
            success: function(data, textStatus, request){
                console.log(data);
            }
 });

When I run this I get 200 OK response and this error in 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.

and this error in 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.

and nothing is logged to the console. Please how can I fix this error?

Thanks.

解决方案

CORS (Cross-Origin Resource Sharing) is an HTML5 feature that allows one site to access another site’s resources despite being under different domain names.

The W3C specification for CORS actually does a pretty good job of providing some simple examples of the response headers, such as the key header, Access-Control-Allow-Origin, and other headers that you must use to enable CORS on your web server.

If you are looking for specific information on how set up CORS on a variety of common web servers, check out the Enable CORS sharing website. http://enable-cors.org/

Based on the URL that you have given above, I don't think you have a control on that server. Thus, it will simply would not work.

Even though you have enabled crossDomain: true the server should return you required headers such as:

Here’s a valid server response; the CORS-specific headers are bolded

HTTP Response:

Access-Control-Allow-Origin: http://xyzcom
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: FooBar
Content-Type: text/html; charset=utf-8

One thing you can try is this:

$.ajax({
            headers: { "Accept": "application/json"},
            type: 'GET',
            url: 'http://cl.ly/2wr4',
            crossDomain: true,
            beforeSend: function(xhr){
                xhr.withCredentials = true;
          },
            success: function(data, textStatus, request){
                console.log(data);
            }
 });

Otherwise, you need to contact API provider.

这篇关于jquery的CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆