36

Is it possible to use jQuery.ajax() in node.js exactly as it is syntax-wise?

I am trying to share non-UI browser code with node.js. I do not want to replace all the existing function calls with my own wrapper.

Currently when I try it, it would say "No Transport" by default because jQuery does domain detection. If I turn it off by setting jQuery.support.cors it would say XMLHttpRequest.open() not available.

2
  • 2
    Overriding the Ajax method of jQuery with a small HTTP client in Node would be a better option than replacing all existing calls in the code, but still hacky. Commented Dec 26, 2011 at 21:58
  • #380 references this issue. Just updated my nq package to workaround this. Commented Oct 29, 2012 at 16:53

5 Answers 5

43

I was able to solve the "No Transport" issue using the XMLHttpRequest module, like this:

var $ = require('jquery'),
    XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

$.support.cors = true;
$.ajaxSettings.xhr = function() {
    return new XMLHttpRequest();
};
Sign up to request clarification or add additional context in comments.

Comments

12

also consider najax, a wrapper for the node request module which allows jquery style syntax for server-side requests

https://github.com/alanclarke/najax

var najax = require('najax');
najax('http://www.google.com', function(html){ console.log(html); });
najax('http://www.google.com', { type:'POST' }, function(html){ console.log(html); });
najax({ url:'http://www.google.com', type:'POST', success: function(html){ console.log(html); });
najax({ url:'http://www.google.com', type:'POST' }).success(function(resp){}).error(function(err){});

najax.get, najax.post, najax.put, najax.delete...

2 Comments

This lead me to jajax and djax which have no dependencies (particularly the bulky lodash) making them better suited for browserify projects.
I eventually ended up not finding a jQuery.ajax equivalent that suited my needs. djax doesn't support the quite convenient complete handler, so I decided to go with future technology: github.com/github/fetch
8

If you want the exact jQuery.ajax syntax, try https://github.com/driverdan/node-XMLHttpRequest

But really if you have control over what you're calling ajax for, you should do it with node's http.request or a module like request

2 Comments

It is not as intuitive as I had though. The node jquery module has built-in support for node-XMLHttpRequest. However, it has been broken by recent version of jsdom.
@voidvector: you are right, jsdom now defines a "noop" XMLHttpRequest function as referenced here: github.com/tmpvar/jsdom/issues/380
7

I am also sharing code between a browser and nodejs and also use JQuery for Ajax calls. JQuery requires a window which I use from domino.

update:

if (typeof module !== 'undefined' && module.exports)
{
  if (typeof global !== 'undefined' && typeof global.process !== 'undefined' &&
    Object.prototype.toString.call(global.process) === '[object process]') {
    var domino = require('domino');
    var window = domino.createWindow('<html></html>');

    var document = window.document;
    var $ = require('jquery')(window);
    var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
    $.support.cors = true; // cross domain, Cross-origin resource sharing
    $.ajaxSettings.xhr = function() {
      return new XMLHttpRequest();
    };
  }
}

2 Comments

Why are you exporting "huepi" when its not defined anywhere?
Good point :). I copied it from my project that uses this, It is called huepi: github.com/arndbrugman/huepi. I'll remove it from the code above. Thx for noticing!
0

You can use Electron, it allows hybrid browserjs and nodejs.

Before, I tried to use canvas2d in nodejs, but finally I gave up. It's not supported by nodejs default, and too hard to install it (many many ... dependeces). Until I use Electron, I can easily use all my previous browserjs code, even WebGL, and pass the result value(eg. result base64 image data) to nodejs code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.