Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I pass request headers with jQuery's getJSON() method?
    text
    copied!<p>I need to do a <code>getJSON()</code> request, but how do I pass authorisation and custom headers?</p> <p>I am getting issues that the request header is taking the name, but NOT the values. The URL is being shown through a manual request in fiddler to being inserted in as options instead of GET/Url.</p> <p>Here is an example of what we are trying to do that works fine in fiddler; how can I replicate this with the AJAX function?</p> <pre class="lang-none prettyprint-override"><code>GET /Service.svc/logins/gdd53535342/houses/vxcbdfsdg/people/dsgsdggd?format=json HTTP/1.1 User-Agent: Fiddler Authorization: Basic rgbg423535fa23y4436 X-PartnerKey: df3fgeg-g5g6-b55b-f3d2-dsgg353523 Host: 154.34.53.54:2757 </code></pre> <p>JavaScript code:</p> <pre><code>xhr = new XMLHttpRequest(); $(document).ready(function() { $.ajax({ url: 'http://localhost:437/service.svc/logins/jeffrey/house/fas6347/devices?format=json', type: 'GET', datatype: 'json', success: function() { alert("Success"); }, error: function() { alert('Failed!'); }, beforeSend: setHeader }); }); function setHeader(xhr) { xhr.setRequestHeader('Authorization', 'Basic faskd52352rwfsdfs'); xhr.setRequestHeader('X-PartnerKey', '3252352-sdgds-sdgd-dsgs-sgs332fs3f'); } </code></pre> <p>Fiddler Normal Request Headers:</p> <pre class="lang-none prettyprint-override"><code>GET /service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1 User-Agent: Fiddler Authorization: Basic faskd52352rwfsdfs X-PartnerKey: 3252352-sdgds-sdgd-dsgs-sgs332fs3f Host: localhost:437 </code></pre> <p>Fiddler Through <code>Ajax()</code> Request Headers:</p> <pre class="lang-none prettyprint-override"><code>OPTIONS service.svc/logins/jeffrey/house/fas6347/devices?format=json HTTP/1.1 Host: localhost:437 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Proxy-Connection: keep-alive Origin: http://ipv4.fiddler:61975 Access-Control-Request-Method: GET Access-Control-Request-Headers: authorization,x-partnerkey </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload