无法在$ ajax发布请求中检索自定义标头

时间:2013-04-19 15:56:52

标签: jquery ajax asp.net-web-api custom-headers

我正在使用.NET Web API。对于授权,我在标题中添加自定义标记,如下所示:

enter image description here

问题是我无法从客户端的响应中检索“授权令牌”。我试过Angular的$ http和jQuery $ ajax。当我使用Fiddler时,我会获得响应中发送的所有标头。包括自定义“Authorization-Token”标题。

这是jQuery $ ajax代码:

$.ajax({
 type: "POST",
 url: "http://localhost:16879/api/authentication/register",
 data: {
 Email: "blablagmail.com",
 Password: "password1"
}
}).complete(function( a, b) {
 console.log(b.getAllResponseHeaders();
});

结果:

The result of the ajax request

使用Fiddler的结果:

enter image description here

是否可以获取$ ajax请求中从服务器发送的所有标头?

解决方法是在主体中实际发送令牌,并在从客户端执行另一个请求时设置自定义头。但我希望通过在响应的标题中获取“授权令牌”来使其工作。

1 个答案:

答案 0 :(得分:2)

您最好使用body来传递OAuth 2.0 resource owner password grant

中也建议的访问令牌

我看到一些线程在谈论,当响应不成功时,jquery可能无法设置自定义标头。

相关问题