使用jQuery进行CORS身份验证

时间:2013-11-05 20:58:09

标签: javascript jquery ajax authentication cors

我已经阅读了很多关于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标头。

1 个答案:

答案 0 :(得分:4)

检查this

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

Access-Control-Allow-Origin: *

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

相关问题