CORS标题'Access-Control-Allow-Origin'缺失 [英] CORS header 'Access-Control-Allow-Origin' missing

查看:454
本文介绍了CORS标题'Access-Control-Allow-Origin'缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从asp.net表单调用此函数,并在调用ajax时在firebug控制台上收到以下错误。


原始请求已阻止:同源策略不允许在 http://anotherdomain/test.json 上阅读远程资源。 (原因:缺少CORS标头'Access-Control-Allow-Origin')。




  var url ='http://anotherdomain/test.json'; 
$ .ajax({
url:url,
crossOrigin:true,
type:'GET',
xhrFields:{withCredentials:true},
accept:'application / json'
})。done(function(data){
alert(data);
})。fail(function(xhr,textStatus,error){
var title,message;
switch(xhr.status){
case 403:
title = xhr.responseJSON.errorSummary;
message ='请登录到您的服务器';
break;
默认值:
title ='无效的URL或跨源请求被阻止';
message ='你必须显式地添加这个网站'+ window.location.origin +')到您的服务器中允许的网站列表中。';
break;
}
});

我用替代方法,但仍然找不到解决方案。



注意:我没有服务器权限可以更改服务器端(API / URL)。

解决方案

p>这通常发生在您尝试访问其他域的资源时。



这是一个安全功能,用于避免每个人都可以自由访问该域的任何资源在海盗域上拥有完全相同的网站副本。)



当代码发送请求时,您的浏览器中将显示一个200 OK响应代码控制台,这意味着资源被访问,但它不授予共享该资源的权利。这是通过不允许访问控制允许原因。



要改变,你必须写在请求的域文件的.htaccess notyourdomain.com):

 < IfModule mod_headers.c> 
标题集Access-Control-Allow-Origin*
< / IfModule>另一个解决方案是将文件手动复制到您的服务器/域:) $ b



$ b

和平和代码;)


I'm calling this function from my asp.net form and getting following error on firebug console while calling ajax.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://anotherdomain/test.json. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

var url= 'http://anotherdomain/test.json';
        $.ajax({
            url: url,
            crossOrigin: true,
            type: 'GET',
            xhrFields: { withCredentials: true },
            accept: 'application/json'
        }).done(function (data) {
            alert(data);                
        }).fail(function (xhr, textStatus, error) {
            var title, message;
            switch (xhr.status) {
                case 403:
                    title = xhr.responseJSON.errorSummary;
                    message = 'Please login to your server before running the test.';
                    break;
                default:
                    title = 'Invalid URL or Cross-Origin Request Blocked';
                    message = 'You must explictly add this site (' + window.location.origin + ') to the list of allowed websites in your server.';
                    break;
            }
        });

I've done alternate way but still unable to find the solution.

Note: I've no server rights to make server side(API/URL) changes.

解决方案

This happens generally when you try access another domain's resources.

This is a security feature for avoiding everyone freely accessing any resources of that domain (which can be accessed for example to have an exact same copy of your website on a pirate domain).

When the code send the request, you shall have a "200 OK" response code in your browser console, which means that the resource is accessed, but it does not grant the right to share that resource. It does this by not allowing "Access-Control-Allow-Origin".

To change that, you have to write this in the .htaccess of the requested domain file (notyourdomain.com):

    <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    </IfModule>

Another solution is to manually copy the file to your server/domain :)

Peace and code ;)

这篇关于CORS标题'Access-Control-Allow-Origin'缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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