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

 }
});
2
  • why you didn't use the headers property of ajax request since you use jQuery? Commented Jun 10, 2015 at 23:47
  • also note that your header is a string, so setRequestHeader is not taking two parameters! Commented Jun 10, 2015 at 23:49

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick response. headers: { 'Authorization': 'Basic 0JRGDJW587832' } will not work because headers is undefined.
@user1837676,then, try giving two parameters to setRequestHeader.
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.
VM1042:8 Uncaught ReferenceError: header is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.