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

标签 javascript jquery ajax cors preflight

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

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 header 。

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

关于javascript - 对 CORS 预检的响应具有 HTTP 状态代码 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46148555/

相关文章:

javascript - jQuery 延迟函数内的语句静默失败

javascript - Chart.js 条形图 - 分组和映射数据

javascript - jQuery 表单提交事件根本不起作用

Javascript结束ajax-loader.gif

javascript - 使用 ajax post 响应时出现错误 block

javascript - Vanilla JavaScript 异步多个 xhr.open()

javascript - Meteor:模板中的动态图像

javascript - 即使我使用 Number() 函数,数组的最后一个元素始终为 NaN

javascript - 在 Backbone 中动态更改 url

javascript - 设置 StackoverFlow 搜索框