0

I've to append a custom Authentication header (among others) to all of my jQuery Ajax requests.

I know that this can be accomplished using:

beforeSend(jqXHR, settings) Function

A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and settings maps are passed as arguments.

However, reading the XMLHttpRequest object documentation (superset of the XMLHTTPRequest object) there is a line that actually scares me:

Note that AJAX functions only return or pass an XHR object when an XHR object is actually used in the request. For example, JSONP requests and cross-domain GET requests use a script element rather than an XHR object.

What's the meaning of CORS GET requests uses a script rather than an XHR object? Does it means that setting custom headers is not possible with GET requests?

gremo
  • 47,186
  • 75
  • 257
  • 421

1 Answers1

1

Sending custom headers should be possible in a GET request - JSONP uses script injection (a script tag is inserted in your document dynamically) rather than XMLHttpRequest

Adam Baxter
  • 1,907
  • 21
  • 41
  • This sounds good, i don't use JSONP... but what's the meaning of the phrase? – gremo Aug 13 '12 at 23:58
  • It means that the request is completed using a – Adam Baxter Aug 14 '12 at 00:27
  • So, if i understand correctly, that wouldn't matter; even with CORS, i'll fill jqXHR with my headers and the send the request; jQuery will do the rest (either using a – gremo Aug 14 '12 at 00:30
  • If you selected JSONP you wouldn't be able to add headers as that's not within JQuery's control, but if you're using the jqXHR object, yes you can add headers. Try it and see! – Adam Baxter Aug 14 '12 at 00:33