【发布时间】:2014-09-12 10:57:42
【问题描述】:
我正在尝试获取 xhr 请求。 (脚本应该每 2 秒循环运行一次) 这是我的脚本:
function getJson() {
var xhr = new XMLHttpRequest();
xhr.open("get", "http://www.oref.org.il/WarningMessages/alerts.json", true);
xhr.onload = function(){
var response = JSON.parse(xhr.responseText);
checkJson(response);
}
xhr.send(null);
setTimeout(arguments.callee, 2000);
}
getJson();
我收到此错误:XMLHttpRequest cannot load http://www.oref.org.il/WarningMessages/alerts.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://klh-dev.com' is therefore not allowed access.
所以我在网上搜索并尝试在脚本中添加几行,但没有奏效:
response.addHeader("Access-Control-Allow-Origin", "http://www.oref.org.il/WarningMessages");
response.addHeader("Access-Control-Allow-Origin", "*");
我在外部 html 页面中尝试了这个
header('Access-Control-Allow-Origin: *');
没有任何效果..
【问题讨论】:
-
@MikeBell 说的是真的。这些标头必须在服务器端设置,而不是客户端。
标签: javascript http xmlhttprequest request