Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#8318 closed bug (invalid)

Incorrect headers are sent when performing cross-domain ajax request

Reported by: anonymous Owned by: anonymous
Priority: low Milestone: 1.next
Component: ajax Version: 1.5
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by jitter)

When I perform cross-domain ajax request:

    success:function(data, textStatus, jqXHR) {

Script that sends response is:

header('Access-Control-Allow-Origin: *');
echo file_get_contents('cross.json');

cross.json is:

    "name": "konst"

When I do GET request, everything is fine. When I do POST, request is not sent and in chrome it says:

XMLHttpRequest cannot load http://fbtest/cross.php. Request header field x-requested-with is not allowed by Access-Control-Allow-Headers.

I've tracked down the code and found these lines in development version of jquery 1.5:

// Requested-With header
// Not set for crossDomain requests with no content
// (see why at http://trac.dojotoolkit.org/ticket/9486)
// Won't change header if already provided
if ( !( s.crossDomain && !s.hasContent ) && !headers["x-requested-with"] ) {
	headers[ "x-requested-with" ] = "XMLHttpRequest";

So you've got it right - you should NOT set x-requested-with header for cross-domain request. However you do. When I comment out this logic, it works fine. I think there is something wrong with checks you do.

Change History (9)

comment:1 Changed 13 years ago by jitter

Component: unfiledajax
Description: modified (diff)
Priority: undecidedlow

comment:2 Changed 13 years ago by jaubourg

Requests with a body will issue a preflight request. Your script doesn't set Access-Control-Allow-Headers for the preflight request. So "you've got it right" - you SHOULD set Access-Control-Allow-Headers properly during the preflight request: http://www.w3.org/TR/cors/#access-control-allow-headers-response-he

X-Requested-With is important for services that want to allow/disallow specific requesters and you should be using Access-Control-Allow-Headers since you'll need it whenever you have to pass custom headers.

comment:3 Changed 13 years ago by jitter

Owner: set to anonymous
Status: newpending

comment:4 Changed 13 years ago by anonymous

Status: pendingnew

Google took me here ... jauborg: Can you specify HOW you'd "set Access-Control-Allow-Headers properly during the preflight request" ? I've tried a number of header combinations in my serverside PHP script, and still can't get things working properly. What's the correct way of doing this type of request? We're not the only ones with this problem it seems:


comment:5 Changed 13 years ago by anonymous

Ok, got it ... You need to add <?php header("Access-Control-Allow-Headers: x-requested-with"); ?>

To your serverside script. Sorry about my whinging ;)

comment:6 Changed 13 years ago by snover

Resolution: invalid
Status: newclosed

comment:7 Changed 12 years ago by matas

I'm sorry to intervene, but no way this ticket is closed/invalid: The jxhr should never set any X- header for the crossDomain requests, it says it in the script comment too:

if ( !s.crossDomain && !headersX-Requested-With? ) {

headers[ "X-Requested-With" ] = "XMLHttpRequest";


the bug is somewhere in the setting of the crossDomain when it's a 'post' request. it could also have implications in other code areas where the s.crossDomain property is evaluated.

here's another test script: http://jsfiddle.net/RjVVg/

please change it back to open!

comment:8 Changed 12 years ago by Agos

I'll second the request for reopening. A server-side workaround is not enough, think of third-party APIs.

comment:9 Changed 12 years ago by Agos

Whoops sorry, seems like jQuery 1.5.2 fixes this.

Note: See TracTickets for help on using tickets.