我目前遇到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
我该怎么办?谢谢你的帮助。
答案 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() {
});