【发布时间】:2018-02-19 06:33:58
【问题描述】:
这几乎是一个已知的标准错误,但无法使用现有的 Stackoverflow 帖子修复。
XMLHttpRequest 无法加载https://myserver.com/context/
对预检请求的响应未通过访问控制检查:
请求中不存在“Access-Control-Allow-Origin”标头 资源。
因此,Origin 'https://www.otherwebsite.com' 不允许访问。
响应的 HTTP 状态代码为 405。
以下是我的代码 -
function setHeader(xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
}
var url = "https://myserver.com/context/";
$.ajax({
url: url,
type: 'GET', dataType:'json',
success: function(data) {
_this.question(data.question);
_this.questionId(data.questionId);
_this.choices(data.choices);
}, error: function() {
console.log("ERROR in getting microquestion");
},
beforeSend: setHeader
});
【问题讨论】:
-
读取错误消息:“没有'Access-Control-Allow-Origin'标头存在请求的资源。”
-
我刚刚意识到试图指出什么错误。问题出在服务器端。这就是“请求的资源”的意思。感谢@sideshowbarker 明确指出了这一点。为你们俩点赞
标签: javascript jquery ajax cors preflight