从Ajax发送跨域请求中的标头 [英] Send Headers in Cross domain request From Ajax

查看:113
本文介绍了从Ajax发送跨域请求中的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 UserID 作为请求标头传递,但我无法通过跨域网址中的请求标头传递它。

I need to pass UserID as request header but I am not able to pass it with the request headerin the cross domain url.

下面是Ajax请求的屏幕截图,其中我得到 Accept-Control-Request-Header:UserID 但是当我使用<传递它时a href =http://api.jquery.com/jquery.ajax/ =nofollow noreferrer> $。ajax()headers 选项;它没有显示在Firebug的 Headers选项卡

Below is the screenshot for the Ajax Request in which I am getting the Accept-Control-Request-Header : UserID but when I pass it using the $.ajax() headers option; it not not shown in the Headers Tab of Firebug

我还尝试了 beforeSend 选项但没有进展。

I also tried the beforeSend option but no progress.

以下是我使用的示例代码,

Below is the sample code which I have used,

headers: {'UserID': 12345 },
dataType: 'jsonp',
crossdomain: true,
beforeSend: function (request) {                
      request.withCredentials = true;
      request.setRequestHeader("UserID", "1");
      request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
      request.setRequestHeader("Access-Control-Allow-Origin", "*");
},

我已经推荐了以下所有SO网址,

And I have referred all the below SO Urls,


  1. jQuery JSONP ajax,未设置身份验证标头

  2. 我可以在跨域json请求中设置标头吗?

  3. 修改JSONP请求的HTTP标头

  1. jQuery JSONP ajax, authentication header not being set
  2. Can I set headers in cross domain json requests?
  3. Modify HTTP Headers for a JSONP request



<有没有办法在 Firebug Headers选项卡中显示请求标头,或者是阻止请求标头的服务器端安全性。

Is there a way to show the request headers in the Firebug Headers tab or it is the Server Side Security which blocks the request header.

推荐答案

最后,我得到了解决方案。

Finally, I got the solution.

我的结尾没有错误(客户端) 。实际上错误来自服务器端,这不允许我发送标头。在服务器端允许交叉原始请求参数问题已解决。

There was no error at my end(client end). Actually the error was from server side, which was not allowing me to send headers. Allowing cross origin request parameters at server end the problem is solved.

这篇关于从Ajax发送跨域请求中的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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