jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.12k stars 270 forks source link

XHR response.responseXML #81

Closed jonahsimpson closed 10 years ago

jonahsimpson commented 10 years ago

Hi!

We are using xdomain.js with some legacy code that manually calls xhr and have noticed a bug.

The xhr call is getting some xml content and reading the result out of response.responseXML. When we execute without xdomain.js the data is in response.responseXML as expected. However, if we include xdomain.js and make the same call, response.responseXML returns null.

We've tracked this problem down to the writeBody() function:

writeBody = function() {
    if (response.hasOwnProperty('text')) {
      facade.responseText = response.text;
    } else if (response.hasOwnProperty('xml')) {
      facade.responseXML = response.xml;
    } else {
      facade.response = response.data || null;
    }
  };

As far as I can tell, response.text will always exist when response.xml exists. If we modify the writeBody method to look something like the following, the problem goes away.

writeBody = function() {
    if (response.hasOwnProperty('text')) {
      facade.responseText = response.text;
    } 
    if (response.hasOwnProperty('xml')) {
      facade.responseXML = response.xml;
    }
    facade.response = response.data || null;
  };

Can you confirm that this is a legit fix? Sorry, I woulda have done a pull request, but I don't know coffeescript.

Thanks in advance!

jpillora commented 10 years ago

Thanks for the report Jonah, it's interesting, I've had a PR to make writeBody to change it to the way it is now... I have a feeling there's a browser difference somewhere here. Which browsers and versions is this happening on?

jonahsimpson commented 10 years ago

IE 8 and 9. Note - this only occurs with the legacy code that is expecting a responseXML property when making a request for XML content (it's setting the headers and everything properly). The same call, executed with jQuery instead of custom XHR code, returns data as expected.

jonahsimpson commented 10 years ago

Do you remember what issue the original PR was solving?

jpillora commented 10 years ago

Hey @jonahsimpson your fix is in, it actually went it a little while ago I just forgot to comment!