Otgonbayar Lamjav Otgonbayar Lamjav - 15 days ago 5
Javascript Question

$.ajax like function for Node js?

is Node js has any function just like $.ajax? I think Node js is fully javascript written and $.ajax jquery is also is fully javascript written. Then maybe node js has any function just like $.ajax. Is it wrong?

Answer

Technically, AJAX is a browser-only thing based on a particular API in the browser. So, I will assume that what you're really asking about is for a simple way to make HTTP requests of other HTTP servers from within node.js.

To make such a request, you can either use the built-in http.get() (in the http module) or you can use a higher level add-on module [request()]1. The request module is built on top of the http module, but offers many more features and, for many things, it much easier to use.

Among the list of features in the request module, you will find: stream support, form encoding/decoding, http auth, custom headers, OAuth, signing, redirects, queryString, gzip, etc...

Here's an example:

const request = require('request');

request({method: 'GET', uri: 'http://www.google.com'}, function(err, response, body) {
    // handle response here
});
Comments