使用jquery无法发送自定义标头 [英] Send custom header with jquery not working

查看:82
本文介绍了使用jquery无法发送自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将请求标头中的自定义值与Jquery一起发送到我的Web API。当我在Web API中读取请求标头时,它看起来像这样:



Connection\\\\
Accept\r\\\
Accept-Encoding \r\\\
Accept-Language\r\\\
Host\r\\\
User-Agent\r\\\
Access控制请求,Method\r\\\
Origin\r\\\
Access控制请求-Headers\r\\\




从Google Chrome调用网络API时,我无法使用标题找到mykey值。当我使用小提琴来调用Web API时,我会收到自定义密钥。 。我读了Gobal.aspx中的标题



我尝试过:



< script>

$ .ajax({

类型:发布,

crossDomain:true,

url:'http:// localhost:61190 / webapi',

dataType:'json',

cache:false,

标题:{

'mykey':'value',

'Content-Type':'application / json'

}

});

< / script>



我的网页API



protected void Application_PostAuthorizeRequest()

{

string keys =;

for(int i = 0; i< HttpContext.Current.Request.Headers.Count; i ++)

{

keys + = HttpContext.Current.Request.Headers.Keys [i] .ToString()+ Environment.NewLine;

}

抛出新的异常(键);

}

I want to send a custom value in the request header with Jquery to my Web API. When I read the request header in the web API, it looks like this:

Connection\r\nAccept\r\nAccept-Encoding\r\nAccept-Language\r\nHost\r\nUser-Agent\r\nAccess-Control-Request-Method\r\nOrigin\r\nAccess-Control-Request-Headers\r\n

I cannot find mykey value with the header when call the web API from Google Chrome. When I use fiddle to call the web API, I receive the custom key. . I read the header in the the Gobal.aspx

What I have tried:

<script>
$.ajax({
type: "Post",
crossDomain: true,
url: 'http://localhost:61190/webapi',
dataType: 'json',
cache: false,
headers: {
'mykey':'value',
'Content-Type':'application/json'
}
});
</script>

My web API

protected void Application_PostAuthorizeRequest()
{
string keys = "";
for (int i = 0; i < HttpContext.Current.Request.Headers.Count; i++)
{
keys += HttpContext.Current.Request.Headers.Keys[i].ToString() + Environment.NewLine;
}
throw new Exception(keys);
}

推荐答案

.ajax({

类型:发布,

crossDomain:true ,

url:'http:// localhost:61190 / webapi',

dataType:'json',

cache:false,

标题:{

'mykey':'value',

'Content-Type':'application / json'

}

});

< / script>



我的网络API



protected void Application_PostAuthorizeRequest()

{

string keys =;

for(int i = 0; i< HttpContext.Current.Request.Headers.Count; i ++)

{

keys + = HttpContext.Current.Request.Headers.Keys [i] .ToString()+ Environment.NewLine;

}

抛出新的异常(键);

}
.ajax({
type: "Post",
crossDomain: true,
url: 'http://localhost:61190/webapi',
dataType: 'json',
cache: false,
headers: {
'mykey':'value',
'Content-Type':'application/json'
}
});
</script>

My web API

protected void Application_PostAuthorizeRequest()
{
string keys = "";
for (int i = 0; i < HttpContext.Current.Request.Headers.Count; i++)
{
keys += HttpContext.Current.Request.Headers.Keys[i].ToString() + Environment.NewLine;
}
throw new Exception(keys);
}


跨域AJAX请求更加复杂:

AJAX跨域|跨源请求| jQuery CORS [ ^ ]



XHR组件最初会向服务器发出飞行前请求,没有任何数据或自定义标头,以确保服务器上的CORS设置允许请求。



您的服务器无条件地为每个请求抛出异常,因此飞行前请求失败,并且永远不会发送真实请求。



尝试更改代码以检查 HttpContent.Current.Request.HttpMethod 在查找自定义标头之前设置为POST 。



您还需要确保在响应中发送正确的CORS标头。

在ASP.NET Web API中启用跨源请求2 | Microsoft Docs [ ^ ]
Cross-domain AJAX requests are more complicated that that:
AJAX Cross Domain | Cross-Origin Request | jQuery CORS[^]

The XHR component will initially make a "pre-flight" request to the server, without any data or custom headers, to ensure that the CORS settings on the server will allow the request.

Your server is unconditionally throwing an exception for every request, so the pre-flight request fails, and the real request is never sent.

Try changing your code to check that HttpContent.Current.Request.HttpMethod is set to "POST" before looking for your custom header.

You'll also need to make sure you're sending the correct CORS headers in the response.
Enabling Cross-Origin Requests in ASP.NET Web API 2 | Microsoft Docs[^]


这篇关于使用jquery无法发送自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆