jquery跨域认证

han*_*nsi 8 authentication jquery http jetty cross-domain

我将Jetty服务器配置为允许跨域http请求(allowedOrigins =*),并使用其CrossOriginFilter允许跨域身份验证(allowCredentials = true).没有身份验证要求的跨域http请求正常工作.现在谈到需要身份验证的http调用时,使用JQuery无法解决问题.我使用以下代码并遵循此示例:http://www.aswinanand.com/2009/01/http-basic-authentication-using-ajax/

function login(username, password) {
$.ajax({
    type: "GET",
    contentType: "application/json",
    dataType: "json",
    url: url,
    beforeSend: function(xhr) {
        var base64 = Base64.encode(username + ":" + password);
        xhr.setRequestHeader("Authorization", "Basic " + base64);
        xhr.withCredentials = true;
    },
    error: function(data){
        alert("error");
    },
    success: function(data){
        alert("success"); 
    }
});
Run Code Online (Sandbox Code Playgroud)

在HTTPFox中,我看到以下对服务器的请求:

OPTIONS /login HTTP/1.1
...
Access-Control-Request-Method   GET
Access-Control-Request-Headers  authorization,content-type
Run Code Online (Sandbox Code Playgroud)

服务器以a响应

HTTP/1.1 204 No Content
...
Allow   OPTIONS,GET,HEAD
Run Code Online (Sandbox Code Playgroud)

我也使用下面的选项,这没有什么区别.

$.ajax({
    ...
    username: username,
    password: password,
    ...
}
Run Code Online (Sandbox Code Playgroud)

错误功能始终触发.有谁知道问题可能是什么?

han*_*nsi 5

默认允许的标头是

X-要求-着,内容类型,接受,产地

我不得不添加标题

授权,内容类型

通过日志文件找到了这个

DEBUG [2012-05-27 17:04:02,468] org.eclipse.jetty.servlets.CrossOriginFilter:标题[authorization,content-type]不在允许的标题中[X-Requested-With,Content-Type,Accept,Origin ]

谢谢你的所有提示!


归档时间:

查看次数:

5693 次

最近记录:

13 年,2 月 前