【问题标题】:Minimal cross domain ajax example that forces preflight OPTIONS强制预检选项的最小跨域 ajax 示例
【发布时间】:2013-11-30 02:18:33
【问题描述】:

所以,我试图在使用 Express 构建的服务器中结合跨域服务和基本身份验证。对于我在客户端使用的特定路由,请使用以下 ajax 调用:

$.ajax({
    type: "PUT",
    accepts: "application/json",
    url: aURL,
    data: JSON.stringify(toPut),
    contentType: "application/json; charset=UTF-8",
    dataType: "json",
    success: function(data, textStatus, jqXHR) {
        alert("Received success: '" + JSON.stringify(data) + "'");
    },
    error: function (data, textStatus, errorThrown) {
        alert("Received error: '" + JSON.stringify(data) + "'\n Status: '" + textStatus + "'\n error thrown = '" + errorThrown + "'");
    },
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    },
    username: myData.username,
    password: myData.password
}).done(function() {;}).fail(function() {
    alert( "error while putting something" );
}).always(function() {;});

但是,在服务器的控制台中,我并没有真正看到 OPTIONS 请求进入。可能是什么问题?

【问题讨论】:

    标签: jquery ajax cors basic-authentication http-request


    【解决方案1】:

    如果您没有看到 OPTIONS 请求,则说明该请求未发送,或者在到达您的服务器之前被拦截,或者不是跨域请求。任何跨域 PUT 请求都将导致预检 (OPTIONS)、DELETE 或除 POST、GET 或 HEAD 之外的任何其他方法。如果 POST/GET/HEAD 包含 non-simple headers,也会导致预检,例如 Content-Type 为“application/json”的 POST。

    注意跟踪upload progress on cross-domain XHR2 requests will also result in a preflight。

    【讨论】:

    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 2016-11-06
    • 2011-01-02
    • 1970-01-01
    • 2017-05-02
    • 2021-03-12
    • 1970-01-01
    • 2015-08-12
    相关资源
    最近更新 更多