Adding HTTP Header to AJAX get request

4k views Asked by At

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);

 }
});
1

There are 1 answers

5
Adib Aroui On

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!