我从我的asp.net表单调用这个函数,并在调用ajax时在firebug控制台上得到以下错误。
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;
}
});
我已经换了一种方法,但仍然找不到解决办法。
注意:我没有服务器权限进行服务器端(API/URL)更改。
您必须修改服务器端代码,如下所示
在ajax请求中,添加:
后行:
应该解决这个问题。。
希望这对你有帮助
当您尝试访问另一个域的资源时,通常会发生这种情况。
这是一项安全功能,可以避免每个人自由访问该域的任何资源(例如,可以访问该域,以便在盗版域上拥有完全相同的网站副本)。
响应的头,即使是200OK,也不允许其他源(域、端口)访问ressources。
如果您是两个域的所有者,则可以解决此问题:
解决方案1:via.htaccess
要更改此设置,可以将其写入请求域的.htaccess文件中:
解决方案2:正确设置标题
如果将此设置为请求文件的响应头,则允许所有人访问资源:
或者
和平与规范;)
相关问题 更多 >
编程相关推荐