Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Last active October 30, 2019 23:30
Show Gist options
  • Save joe-oli/286934e347250fee9d6944b4a023751b to your computer and use it in GitHub Desktop.
Save joe-oli/286934e347250fee9d6944b4a023751b to your computer and use it in GitHub Desktop.
ajax libs
/* axios --------------------------------------------------
Axios is a promise based HTTP client for the browser and Node.js.
>npm install axios
*/
var request = require('axios');
axios.get('https://localhost:3000/data.json')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
/* Superagent --------------------------------------------------
Superagent is small progressive client-side HTTP request library.
>npm install superagent
*/
var request = require('superagent');
request
.get('https://localhost:3000/data.json')
.end(function(err, res){
console.log(res);
});
/* Request --------------------------------------------------
Request is a simplified HTTP request client.
>npm install request
*/
var request = require('request');
request('https://localhost:3000/data.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response);
}
})
/* whatwg-fetch --------------------------------------------------
a polyfill for the Fetch API. This allows support for more browsers.
This project doesn't work under Node.js environments. It's meant for web browsers only. You should ensure that your application doesn't try to package and run this on the server.
BUT NOTE: modern browsers already have a BUILT-IN FETCH lib !
>npm install whatwg-fetch --save
*/
var fetch = require('whatwg-fetch');
fetch('https://localhost:3000/data.json')
.then(function(response) {
return response.json();
}).then(function(json) {
console.log(json);
}).catch(function(err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment