1

I am trying to make an AJAX call to an API which requires an HTTP header (REST parameters).Currently no data is being returned. I think what is giving the most difficulty is understanding setRequestHeader, not even sure if its necessary. In this example , msdn it takes 2 string arguments: oReq.setRequestHeader("Content-Type", "text/xml") but then where does the authorization header go? Please Help

Currently I have this:

var baseURL = "https://api.azuga.com/azuga-ws/v1/live/location.json";

var header = "Authorization: Basic 0JRGDJW587832"; //Made up number

$.ajax({
      url: baseURL,
      dataType: 'json',
      beforeSend: function(xhr){xhr.setRequestHeader(header);},
      success: function(data){
            console.log(data);

 }
});
Achrome
  • 7,773
  • 14
  • 36
  • 45
Steez
  • 219
  • 5
  • 17

1 Answers1

1

Did you tried:

var baseURL = "https://api.azuga.com/azuga-ws/v1/live/location.json";



$.ajax({
      url: baseURL,
      dataType: 'json',
      headers: { 'Authorization': 'Basic 0JRGDJW587832' },
      beforeSend: function(xhr){xhr.setRequestHeader(header);},
      success: function(data){
            console.log(data);

 }
});

?

also note that your header is a string, so setRequestHeader is not taking two parameters!

Adib Aroui
  • 4,981
  • 5
  • 42
  • 94
  • Thanks for the quick response. headers: { 'Authorization': 'Basic 0JRGDJW587832' } will not work because headers is undefined. – Steez Jun 11 '15 at 00:00
  • http://stackoverflow.com/questions/10093053/add-header-in-ajax-request-with-jquery – Steez Jun 11 '15 at 00:00
  • @user1837676,then, try giving two parameters to `setRequestHeader`. – Adib Aroui Jun 11 '15 at 00:02
  • Tried that as well. Now I am at least receiving a 401 error thats saying im unauthorized. However I've tested the header in googles advances rest client extension and it returns a response. – Steez Jun 11 '15 at 00:09
  • VM1042:8 Uncaught ReferenceError: header is not defined – 2787184 Jan 12 '17 at 06:34