传递请求头中jQuery的AJAX GET调用 [英] Pass request headers in a jQuery AJAX GET call

查看:366
本文介绍了传递请求头中jQuery的AJAX GET调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过请求头中的AJAX GET使用jQuery。在下面的块中,数据自动传入查询字符串的值。有没有办法通过在请求报头中的数据吗?

  $。阿贾克斯({
         网址:HTTP://本地主机/ PlatformPortal /采购商/客户/签到,
         数据:{签名:authHeader},
         键入:GET,
         成功:函数(){警报('!成功'+ authHeader); }
      });
 

下面没有任何工作

  $。阿贾克斯({
         网址:HTTP://本地主机/ PlatformPortal /采购商/客户/签到,
         beforeSend:{签名:authHeader},
         异步:假的,
         键入:GET,
                    成功:函数(){警报('!成功'+ authHeader); }
      });
 

解决方案

使用 beforeSend

  $。阿贾克斯({
         网址:HTTP://本地主机/ PlatformPortal /采购商/客户/签到,
         数据:{签名:authHeader},
         键入:GET,
         beforeSend:功能(XHR){xhr.setRequestHeader(X试验头,测试值);},
         成功:函数(){警报('!成功'+ authHeader); }
      });
 

http://api.jquery.com/jQuery.ajax/

<一个href="http://www.w3.org/TR/XMLHtt$p$pquest/#the-setrequestheader-method">http://www.w3.org/TR/XMLHtt$p$pquest/#the-setrequestheader-method

I am trying to pass request headers in an AJAX GET using jQuery. In the following block, "data" automatically passes the values in the querystring. Is there a way to pass that data in the request header instead ?

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         data: { signature: authHeader },
         type: "GET",
         success: function() { alert('Success!' + authHeader); }
      });

The following didn't work either

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         beforeSend: { signature: authHeader },
         async: false,                    
         type: "GET",
                    success: function() { alert('Success!' + authHeader); }
      });

解决方案

Use beforeSend:

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         data: { signature: authHeader },
         type: "GET",
         beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},
         success: function() { alert('Success!' + authHeader); }
      });

http://api.jquery.com/jQuery.ajax/

http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method

这篇关于传递请求头中jQuery的AJAX GET调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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