【发布时间】: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