【问题标题】:CORS authentication with jQuery使用 jQuery 进行 CORS 身份验证
【发布时间】:2013-11-16 21:48:37
【问题描述】:

我已经阅读了很多关于 CORS 和身份验证的文档,并且我很确定可以正确地做事。我正在尝试验证 jQuery.ajax GET CORS 请求,如下所示:

$.ajax('http://httpbin.org/basic-auth/foo/bar', {
    type: 'GET',
    username: 'foo',
    password: 'bar',
    xhrFields: {
        withCredentials: true
    }
}).then(function (data) {
    alert('success');
}, function (xhr) {
    alert('failure');
});

但浏览器不断提示我输入凭据,而不是发送提供的凭据。在http://jsfiddle.net/BpYc3/ 试试这个,http://httpbin.org 应该发送正确的 CORS 标头。

【问题讨论】:

  • XMLHttpRequest cannot load http://httpbin.org/basic-auth/foo/bar. Credentials flag is true, but Access-Control-Allow-Credentials is not "true". 你确定服务器允许跨域凭据吗?我不必处理这个问题,但由于那个错误,我首先会看。
  • @KevinB 对,httpbin.org 没有为 GET 请求设置该标头,但即使我在我的 API 服务器上设置了该标头,浏览器仍会提示输入凭据。

标签: javascript jquery ajax authentication cors


【解决方案1】:

签出this

如果您的服务器要求提供凭据,那么您将无法使用

Access-Control-Allow-Origin: *

使用通配符:您必须指定允许的域。

【讨论】:

    猜你喜欢
    • 2014-01-04
    • 2012-04-30
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2020-02-28
    相关资源
    最近更新 更多