jsonp request
install
npm i lc-jsonp --save
code example(webpack)
var jsonp = require('lc-jsonp');
var url = 'http://www.example.com';
var arg = {id: 1};
jsonp(url, arg)
.then(function(res){
console.log(res);
}
);
//jsonp is a Promise object, you can use bluebird resolve it and use timeout function.
var Promise = require('bluebird');
var timeout = 300; //milliseconds
Promise.resolve(jsonp(url, arg))
.timeout(timeout)
.then(function(res){
console.log(res);
}
).catch(Promise.TimeoutError, function() {
console.error('Request time out, do something.');
});
- This module will occupy a global variable, named window.loadJsonp. Please keep in mind of it.
- This module only work for CommonJS. Please use webpack etc to use this module.
- Url can not contain query string.
- Please include Promise shim if you run it in old browser.
Install npm packagesnpm i
, then start server npm start
, then open url http://localhost:3011/test/test.html
in browser.