jsdom/jsdom

Replace the no-op XMLHttpRequest with node-XMLHttpRequest

Closed this issue · 9 comments

The adding of a No Operation XMLHttpRequest is causing other add-on to break, namely node-jquery's $.ajax() functionality. Please remove this implementation or replace it with a node-XMLHttpRequest implementation.

To reproduce:


then create and run a JS file with the following content:


(function(){
        var window = require('jsdom').jsdom().createWindow();
        var $ = require('jquery').create(window);
        $.support.cors = true;// enable cross-domain requests
        $.get('http://www.yahoo.com/').done(function(){console.log('success');}).fail(function(){console.log('failure')});
})();

(function(){
        var window = require('jsdom').jsdom().createWindow();
        window.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
        var $ = require('jquery').create(window);
        $.support.cors = true;// enable cross-domain requests
        $.get('http://www.yahoo.com/').done(function(){console.log('success');}).fail(function(){console.log('failure')});
})();

The first item fails because node-jquery uses the XMLHttpRequest implemented in jsdom (which is a noOp). The second item succeeds as I have replaced it explicitly with a working wrapper.

My fork https://github.com/dai-shi/jsdom adds node-XMLHttpRequest functionality and it seems working. It keeps cookies (127.0.0.1 only) for XHR, but it's more or less a hack. Any idea to properly implement this is welcomed.

@dai-shi can you point us to the commit where you add node-XMLHttpRequest? My guess is that your hack will be better than what we have, so we can probably use it :)

Hm, the basic one is very simple: 6936046
After that, I added cookie passing functionality: 6936046 -> 59d865e
(How can I show commit diffs in GitHub?)
Hope it helps.

@domenic I could make the compare view: dai-shi/jsdom@6936046...59d865e
By "seems working", I meant I could successfully run e2e tests in https://github.com/dai-shi/connect-prerenderer

@dai-shi that looks great. I'd love a pull request with that code (all in one commit). Ideally you could add a test that the XHR works, maybe spinning up a server like in the test "redirected_url_equal_to_location_href" in test/jsdom/index.js. Then you could do an XHR from a jsdom window against that server and make sure it gets results.

@domenic OK, I will try it next week.
Let me know if you have any thought about cookie passing in the XHR.

I'm all for cookie passing in XHR! Happy to include that code too.

@domenic I think you can close this issue now.