当使用简单的jQuery脚本从Apache服务器访问数据时,我遇到了CORS策略的问题。
此代码无效:
$.get("http://myurl.com/test.json", function (json) {
coordinates = json;
console.log(coordinates);
}, "json") ;
虽然这完全正常:
$.ajax({
type: 'GET',
url: 'http://myurl.com/test.json',
cache: false,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
}).done(function ($result) {
console.log($result);
});
我从第一个代码段获得的错误:XMLHttpRequest cannot load http://myurl.com/test.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
服务器端.htaccess已正确设置,所有必需的apache模块都已启用,配置也已修改。 任何想法为什么会发生这种情况?
.htacces:
# Always set these headers.
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "GET"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with,
Content-Type, origin, authorization, accept, client-security-token"
RewriteEngine on