选项资源无法加载 - 基本身份验证+ SSL - Chrome / Safari

时间:2012-08-09 15:29:07

标签: jquery google-chrome ssl safari basic-authentication

我目前遇到Chrome和Safari的问题,但Firefox没有。 我只是想向服务器发送一个基本的GET请求。这个使用SSL和base64中编码的"username:password"的基本身份验证。下面是一个例子:

$.ajax({
  url: 'https://(...)',
  type: "GET",
  beforeSend: function(xhr) {
    var base64 = 'dXNlcm5hbWU6cGFzc3dvcmQ=';
    return xhr.setRequestHeader("Authorization", "Basic " + base64);
  }
});

Chrome开发者工具向我显示错误:

OPTIONS https://(...) Resource failed to load 
f.support.ajax.f.ajaxTransport.send
f.extend.ajax

enter image description here

我该怎么办?谢谢你的帮助。

1 个答案:

答案 0 :(得分:4)

    var auth; 

function make_base_auth(user, password) {
    var tok = user + ':' + password;
    var hash = Base64.encode(tok);
    return "Basic " + hash;
}

function getAuthCookie() {

    var cn = "Authorization=";
    var idx = document.cookie.indexOf(cn)
    var end;

    if(idx != -1) {
        var end = document.cookie.indexOf(";", idx + 1);
        if(end == -1)
            end = document.cookie.length;
        return unescape(document.cookie.substring(idx + cn.length, end));
    } else {
        return "";
    }
}

       document.cookie = encodeURIComponent("Authorization") + "=deleted; expires=" + new Date(0).toUTCString(); 
       auth = make_base_auth($.trim($('#email').val()), $.trim($('#password').val()));
   document.cookie = "Authorization=" + auth;


             $.ajax({
        url : BASE_URI + "user",
        method : "GET",
        beforeSend : function(xhr) {
            xhr.setRequestHeader('Authorization', getAuthCookie());
        },
        xhrFields : {
            withCredentials : true
        },
        accept : "application/json",
        contentType : "application/json",
        cache : false,
        cookie : false,
        statusCode : {
            404 : function() {
                console.log("Page Not Found");
            } 
        }

    }).success(function(response) {
        console.log("Login SUCCESS " + JSON.stringify(response));

           }).fail(function(response) {

    }).then(function() {

           });