我正在尝试发送跨源域并添加自定义“授权”标头。
请参阅下面的代码。
Error:
XMLHttpRequest 无法加载 {url}。 Access-Control-Allow-Headers 不允许请求标头字段授权。
function loadJson(from, to) {
$.ajax({
//this is a 'cross-origin' domain
url : "http://localhost:2180/api/index.php",
dataType : 'json',
data : { handler : "statistic", from : from, to : to
},
beforeSend : setHeader,
success : function(data) {
alert("success");
},
error : function(jqXHR, textStatus, errorThrown) {
alert("error");
}
});
}
function getToken() {
var cookie = Cookie.getCookie(cookieName);
var auth = jQuery.parseJSON(cookie);
var token = "Token " + auth.id + ":" + auth.key;
}
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', getToken());
}
我也尝试过:
headers : { 'Authorization' : getToken() },
在ajax请求中。
难道是jquery-ajax框架阻止了跨域认证?我怎样才能解决这个问题?
Update:
顺便问一下:是否有更安全的方法将 auth.key 存储在客户端而不是存储在 cookie 中?
getToken() 将被替换为更复杂的方法,对正文、日期等进行哈希处理。