Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 11 years ago

#8146 closed bug (fixed)

Cross Domain Support broken in jquery 1.5

Reported by: shreddd Owned by: jaubourg
Priority: high Milestone: 1.5.1
Component: ajax Version: 1.5
Keywords: neededdocs Cc: jaubourg
Blocked by: Blocking:

Description

Following up on the following posting in the forum: http://forum.jquery.com/topic/is-cross-domain-support-broken-in-jquery-1-5

When I try to make a cross domain request (using CORS), I cannot make requests that need the "withCredentials" flag

The following code seems to ignore the xhr.withCredentials flag.

    $.ajax({
      url:  cross_domain_url,
      beforeSend: function(xhr) {
        xhr.withCredentials=true;
      },
      success: function(res, textStatus, xhr) {
        alert(res);
      }
    });

This works with jquery-1.4, but the success handler is never triggered in jquery 1.5. I am using firefox 3.6. Thanks -S

Change History (15)

comment:1 Changed 13 years ago by Rick Waldron

Cc: jaubourg added
Component: unfiledajax
Owner: set to shreddd
Priority: undecidedlow
Status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced jsFiddle test case to help us assess your ticket!

Additionally, test against the jQuery 0 GIT version to ensure the issue still exists.

comment:2 Changed 13 years ago by jaubourg

Keywords: regression added
Milestone: 1.next1.5.1
Owner: changed from shreddd to jaubourg
Priority: lowhigh
Status: pendingassigned

I can confirm this bug. The withCredentials property is put on the jXHR object and, as such, is not propagated to the native xhr (because the native xhr is completely hidden now).

I think the best course of action (seeing as there may be other fields settable on xhr in the future) is to provide a new option (something like xhrProperties) that would contain a map of properties to set on the xhr after creation.

This would look like this:

$.ajax({
   xhrProperties: {
      withCredential: true
   }
});

Only workaround as of now is to redefine the xhr method in the settings like this:

$.ajax({
   xhr: function() {
      var xhr = $.ajaxSettings.xhr();
      xhr.withCredential = true;
      return xhr;
   }
});

If there is no objection, I'll work on adding this feature into core since it's obviously a regression.

comment:3 Changed 13 years ago by jaubourg

Resolution: fixed
Status: assignedclosed

Fixes #8146 by introducing the xhrFields option with is a map of fieldName/fieldValue to set on the native xhr. Can be used to set withCredentials to true for cross-domain requests if needed.

Changeset: 5ef7ddc4c55f49cb7767bba99975f2c7ea5422d9

comment:4 Changed 13 years ago by jaubourg

shreddd, can you confirm your issue is solved if you set the xhrFields option, using git0?

Sample code:

$.ajax({
   xhrFields: {
      withCredentials: true
   }
});

git0: http://code.jquery.com/jquery-git.js

comment:5 in reply to:  4 Changed 13 years ago by shreddd

Fix confirmed to be working in Firefox.

Fix does not work in Chrome: Line 7211 throws an exception:

xhr[ i ] = s.xhrFields[ i ];

Error: INVALID_STATE_ERR: DOM Exception 11

Replying to jaubourg:

shreddd, can you confirm your issue is solved if you set the xhrFields option, using git0?

Sample code:

$.ajax({
   xhrFields: {
      withCredentials: true
   }
});

git0: http://code.jquery.com/jquery-git.js

comment:6 Changed 13 years ago by jaubourg

Resolution: fixed
Status: closedreopened

Got it: http://code.google.com/p/v8/issues/detail?id=789

Should be set after the xhr has been opened. On it.

comment:7 Changed 13 years ago by jaubourg

Status: reopenedassigned

comment:8 Changed 13 years ago by jaubourg

Resolution: fixed
Status: assignedclosed

Fixes #8146. Custom fields in xhrFields are now set after the XMLHttpRequest object has been opened.

Changeset: 823eecab9073b43a283a2500e9e43c3a06cc2495

comment:9 Changed 13 years ago by jaubourg

shreddd: should be all good now :)

comment:10 Changed 13 years ago by jaubourg

Keywords: needsdocs added

comment:11 in reply to:  9 Changed 13 years ago by shreddd

Tested on Firefox and Chrome. Appears to be working. Thanks for the excellent response time on the bug fix.

Replying to jaubourg:

shreddd: should be all good now :)

comment:12 Changed 12 years ago by addyosmani

Keywords: neededdocs added; regression needsdocs removed

Docs were updated during the 1.5.1 sprint, but I've added an example and further notes about 1.5 compatibility with the flag here: http://api.jquery.com/jQuery.ajax/

comment:13 Changed 11 years ago by bernhardeckl@…

It seems that this issue is still not solved. The success function will not get executed.

Here is my code:

jQuery.ajax({

type: 'POST', url: $this.attr('action'), data: $this.serialize(), success: function(data) {

console.log("Success"); alert(data); if(data.redirect){

window.location.href=response.redirect;

}

}, xhrFields: {

withCredentials: true

}

});

comment:14 Changed 11 years ago by anonymous

Hm, that’s strange, the fail handler does something. But why fail?

jQuery.ajax({

type: 'POST', url: $this.attr('action'), data: $this.serialize(), xhrFields: {

withCredentials: true },

success: function(data) {

console.log("Success"); alert(data); if(data.redirect){

window.location.href=response.redirect;

}

}

}).fail(function() {

alert('SAdf'); });

comment:15 Changed 11 years ago by dmethvin

Please take your debugging progress to the forum, rather than posting on a closed ticket.

Note: See TracTickets for help on using tickets.