27

I'm trying to make an ajax request to the google contacts API with the following setup:

$.ajax({
  url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all",
  dataType: 'jsonp',
  data: {
    alt: 'json-in-script'
  },
  headers: {
    'Authorization': 'Bearer ' + token
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  }
});

But the Authentication header doesn't seem to get set. Any ideas?

The request

2
  • What version of jQuery are you using? Commented Sep 15, 2011 at 15:52
  • jQuery .ajax does have 'username' and 'password' fields that will do basic auth for you if needed Commented Sep 19, 2013 at 2:21

4 Answers 4

26

I had the same problem recently. Try this:

$.ajax({
  url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all",
  dataType: 'jsonp',
  data: {
    alt: 'json-in-script'
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  },
  beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + token); } 
});

EDIT: Looks like it can't be done with JSONP. Modify HTTP Headers for a JSONP request

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

1 Comment

xhr.setRequestHeader doesn't work either. Looks like I need to have a read of the question you linked to.
4

When authentication is needed in a cross domain request, you must use a proxy server of some sort.

Since using dataType: jsonp results in the HTTP request actually being made from the script that gets added to the DOM, the headers set in the $.ajax will not be used.

Comments

0

Is seems that most of the OAUTH2 REST resources accept the access_token parameter as part of the request url

http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param

please, try the following code instead:

$.ajax({
            dataType: 'jsonp',
            url: url,                
            data: {
                'access_token':token.access_token
            },
            jsonpCallback: 'thecallback',
            success: function(data){
                _cb(data);
            },
            error: function(d){
                _cb(d);
            }
        });

Comments

-2

Just do this (jquery 2.0, but should work in previous versions)

    $.ajax({
        url: "/test",
        headers: {"Authorization": "Bearer " + $('#myToken').val()}
    })           
    .done(function (data) {
      console.log(data);
    })
    .fail(function (jqXHR, textStatus) {
      alert("error: " + textStatus);
    });

3 Comments

OK, but this is not JSONP ajax request. This is plain ajax request.
the headers option should be valid even if its JSONP. Request headers are part of the HTTP protocol, JSONP is a app/browser level hack.
@rynop "should be valid" doesn't always means "it is valid", the OP's question is very clear and produceable

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.