How to pass request headers using jQuery getJSON () method?

I need to execute a getJSON() request, but how do I go through authorization and custom headers?

I get problems that the request header takes a name but NOT a value. The URL is displayed using a manual request in fiddler to be inserted as parameters instead of GET / Url.

Here is an example of what we are trying to do that works fine in the violinist; How can I reproduce this using the AJAX function?

 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 

JavaScript 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'); } 

Fiddler Normal Request Headers:

 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 

Violinist through Ajax() Request headers:

 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 
+55
jquery getjson
Jul 12 '10 at 15:38
source share
3 answers

I agree with sunetos that you will need to use the $ .ajax function to pass request headers. To do this, you need to write a function for the beforeSend event handler, which is one of the $ .ajax () parameters. Here is a quick example of how to do this:

 <html> <head> <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $.ajax({ url: 'service.svc/Request', type: 'GET', dataType: 'json', success: function() { alert('hello!'); }, error: function() { alert('boo!'); }, beforeSend: setHeader }); }); function setHeader(xhr) { xhr.setRequestHeader('securityCode', 'Foo'); xhr.setRequestHeader('passkey', 'Bar'); } </script> </head> <body> <h1>Some Text</h1> </body> </html> 

If you run the code above and watch the traffic in a tool like Fiddler, you will see two request headers sent to:

  • securityCode with value Foo
  • passkey with a value of Bar

The setHeader function can also be built into the $ .ajax option, but I wanted to call it.

Hope this helps!

+68
Jul 12 '10 at 17:52
source share

I think you could set the headers and use getJSON () like this:

 $.ajaxSetup({ headers : { 'Authorization' : 'Basic faskd52352rwfsdfs', 'X-PartnerKey' : '3252352-sdgds-sdgd-dsgs-sgs332fs3f' } }); $.getJSON('http://localhost:437/service.svc/logins/jeffrey/house/fas6347/devices?format=json', function(json) { alert("Success"); }); 
+23
Feb 15 '14 at 18:18
source share

The $ .getJSON () method is a shorthand that does not allow you to specify additional parameters. For this you need to use the full $ .ajax () method. Check out the documentation at http://api.jquery.com/jQuery.getJSON/ , "This is an abbreviated Ajax function that is equivalent to:"

 $.ajax({ url: url, dataType: 'json', data: data, success: callback }); 

So just use $ .ajax () and specify all the additional parameters you need.

+11
Jul 12 2018-10-12T00:
source share



All Articles