Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Headers with jQuery.ajax and JSONP?

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

like image 629
Lance Avatar asked Jun 18 '10 21:06

Lance


People also ask

Can JSONP be used with Ajax?

JSONP allows you to sidestep the same-origin policy and to some extent make cross-domain Ajax calls. It's not a silver bullet, and it certainly has its issues, but in some cases it can prove invaluable when fetching data from a different origin.

Why do we use AJAX?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.


1 Answers

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.

like image 150
SLaks Avatar answered Oct 08 '22 20:10

SLaks