ajax jquery跨域调用不发送授权头
Posted
技术标签:
【中文标题】ajax jquery跨域调用不发送授权头【英文标题】:ajax jquery cross domain call does not send authorization header 【发布时间】:2013-11-03 10:03:14 【问题描述】:在我对具有服务堆栈的服务器进行跨域调用之前,我已成功通过身份验证并获取我的令牌。
现在我想再调用一次来检索数据:
$.ajax(
beforeSend: function(xhr)
xhr.setRequestHeader('Authorization', 'Basic ' + getToken());
,
type: "GET",
url: requestUrl,
xhrFields:
withCredentials: true
,
async: true,
dataType: 'json',
crossDomain: true
)
当我查看我的谷歌浏览器开发工具控制台时,我看到了这个:
OPTIONS http://MyPc.company:82//customers 404 (Not Found)
OPTIONS http://MyPc.company:82//customers Invalid HTTP status code 404
XMLHttpRequest cannot load http://MyPc.company:82//customers.
Invalid HTTP status code 404 (index):1
当我查看提琴手时,我看到了这个请求:
Inspectors => Auth:不存在授权标头。
检查员 => 原始:
OPTIONS http://MyPc.company:82//customers HTTP/1.1
Host: MyPc.company:82
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Access-Control-Request-Method: GET
Origin: http://MyPc.company
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Access-Control-Request-Headers: access-control-allow-origin, accept, access-control-allow-headers, authorization, access-control-allow-methods, content-type
Accept: */*
Referer: http://MyPc.company/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
为什么没有发送授权标头?乍一看,这似乎是我的起源问题。
【问题讨论】:
您无法控制通过预检 (OPTIONS) 请求发送的标头。在此处阅读预检:developer.mozilla.org/en-US/docs/HTTP/… @RayNicholus “...由于设置了自定义标头,因此预检了此请求。”所以现在我知道我做了一个预检请求,因为我使用了一个授权头?但是要不然怎么把token发回token去查呢? 你能看一下这个answer,也许它可以解决你的问题。 你解决过这个问题吗? 【参考方案1】:在 javascript 中
jQuery.support.cors = true;
function make_base_auth(user, password)
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " + hash;
function DoTest()
var TestRequest = new Object();
TestRequest.name = "Harry Potter";
TestRequest.Id = 33;
var username = "admin";
var password = "test";
$.ajax(
type: 'Post',
contentType: 'application/json',
cache: false,
async: false,
url: serverIP + '/TestAPI/'+ TestRequest.Id,
data: JSON.stringify(TestRequest),
dataType: "json",
beforeSend: function (xhr)
xhr.setRequestHeader("Authorization", make_base_auth(username, password));
,
success: function (response, status, xhr)
var s= response.message;
,
error: function (xhr, err)
alert(xhr.statusText);
);
应该为 CORS 启用服务配置,例如
Plugins.Add(new CorsFeature(allowedHeaders: "Content-Type, Authorization"));
也许我之前的answer 可以帮到你。
或者更好的是Community Resources的以下博客文章
CORS BasicAuth on ServiceStack with custom authentication
【讨论】:
我已经比较了你的 JS 代码和我的代码,除了你没有设置 crossDomain = true 之外,所有重要的东西似乎都一样。当我查看您的 Servicestack 配置时,您是对的,我不记得 CorsFeature 将 Authorization 设置为 allowedHeaders ,但我怀疑这是我的问题。我的问题是根本没有发送任何标头,这意味着我可以在服务器端配置我想要的内容吗?但我仍然会根据您的提示检查我的服务配置。 我刚刚看到你也有:jQuery.support.cors = true;所以我想这和我设置的 crossDomain = true 一样? 我认为是服务配置。如果您将服务中的 [Authenticate] 属性注释掉,它是否正常工作?关于 jQuery.support.cors = true;是的。 您确定您的服务配置为像 here 这样的 CORS 吗?提琴手的请求只是选项。 我现在已经在服务器端尝试过了:Plugins.Add(new CorsFeature(allowedOrigins: Settings.Default.SmartAllowedCorsOrigin, allowCredentials: true, allowedHeaders: "Authorization"));但是授权标头没有像我之前提到的那样发送,这是最初的问题。以上是关于ajax jquery跨域调用不发送授权头的主要内容,如果未能解决你的问题,请参考以下文章