2012-08-09 26 views
3

我目前遇到Chrome和Safari的问题,但与Firefox没有关系。 我只是试图发送一个基本的GET请求到服务器。这一个使用SSL和在base64中编码的"username:password"的基本认证。下面一个例子:选项资源加载失败 - 基本身份验证+ SSL - Chrome/Safari

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

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

+0

在我看来像XSS保护。即使这个错误看起来很奇怪。 为了克服,你将不得不使用jsonp。但是,这意味着你必须将用户/密码输入到url明文中,所以它不是真正的解决方案。 – dualed 2012-08-19 16:26:36

回答

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

      });