对 CORS 预检的响应具有 HTTP 状态代码 405

Response to CORS preflight has HTTP status code 405

这几乎是一个已知的标准错误,但无法使用现有的 Whosebug 帖子修复它。

XMLHttpRequest cannot load https://myserver.com/context/
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'https://www.otherwebsite.com' is therefore not allowed access.
The response had HTTP status code 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
});

“对预检请求的响应……有 HTTP 状态代码 405。” 消息表明,要开始,需要配置 https://myserver.com/context/ 端点来处理 OPTIONS 请求正确——即使它可能已经设置为发送正确的 CORS headers 返回“正常”请求。

有关正在发生的事情的更多详细信息,您可以阅读 No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API[=22 中答案的 如何避免 CORS 预检 部分=] — 但其要点是,至少,服务器必须使用 2xx 成功状态代码响应 OPTIONS 请求。