Bug Tracker

Ticket #8146 (closed bug: fixed)

Opened 3 years ago

Last modified 18 months ago

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
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by rwaldron

  • Cc jaubourg added
  • Owner set to shreddd
  • Status changed from new to pending
  • Component changed from unfiled to ajax
  • Priority changed from undecided to low

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 3 years ago by jaubourg

  • Keywords regression added
  • Owner changed from shreddd to jaubourg
  • Priority changed from low to high
  • Status changed from pending to assigned
  • Milestone changed from 1.next to 1.5.1

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 3 years ago by jaubourg

  • Status changed from assigned to closed
  • Resolution set to fixed

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 follow-up: ↓ 5 Changed 3 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 3 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 3 years ago by jaubourg

  • Status changed from closed to reopened
  • Resolution fixed deleted

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 3 years ago by jaubourg

  • Status changed from reopened to assigned

comment:8 Changed 3 years ago by jaubourg

  • Status changed from assigned to closed
  • Resolution set to fixed

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

Changeset: 823eecab9073b43a283a2500e9e43c3a06cc2495

comment:9 follow-up: ↓ 11 Changed 3 years ago by jaubourg

shreddd: should be all good now :)

comment:10 Changed 3 years ago by jaubourg

  • Keywords regression, needsdocs added; regression removed

comment:11 in reply to: ↑ 9 Changed 3 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 3 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 18 months 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 18 months 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 18 months 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.