46

I am trying to access google docs with jQuery. Here's what I have so far:

var token = "my-auth-token";
$.ajax({
  url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json",
  dataType: 'jsonp',
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
  },
  success: function(data, textStatus, XMLHttpRequest) {
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
  }
});

It doesn't allow me to set headers if I set the dataType to jsonp (from Make Cross Domain Ajax Requests with jQuery). If I leave out jsonp, I can't make the cross-domain request. If I use jQuery.getJSON, I can't pass in any headers...

Is there any way to define custom headers when making a cross-domain ajax request (in jQuery)?

Lance
  • 75,200
  • 93
  • 289
  • 503

1 Answers1

69

This is not possible.

A JSONP request works by creating a <script> element with its src attribute set to the request URL.
You cannot add custom headers to the HTTP request sent by a <script> element.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    thanks, great to know. follow-up question: http://stackoverflow.com/questions/3073336/how-to-make-cross-domain-requests-in-jquery-and-set-custom-headers. – Lance Jun 18 '10 at 22:02