3

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

$.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

在此处输入图像描述

我能做什么 ?谢谢你的帮助。

4

1 回答 1

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() {

           });
于 2012-08-20T08:08:48.457 回答