[fix] params serialize

pull/1/head
Ziqiang Li 2017-12-11 14:14:26 +08:00
parent 2ae82c15f8
commit 9b480dd19f
3 changed files with 50 additions and 36 deletions

View File

@ -46,7 +46,7 @@ function jsonp(url, options) {
} }
function serialize(params) { function serialize(params) {
var param = '?'; var param = '';
for (var key in params) { for (var key in params) {
if (params.hasOwnProperty(key)) { if (params.hasOwnProperty(key)) {
param += '&' + key + '=' + encodeURIComponent(params[key]); param += '&' + key + '=' + encodeURIComponent(params[key]);
@ -55,6 +55,14 @@ function jsonp(url, options) {
return param; return param;
} }
function handleUrl(url, params) {
if (!~url.indexOf('?')) {
url += '?';
}
url += serialize(params);
url = url.replace('?&', '?');
}
promise = new Promise(function (resolve, reject) { promise = new Promise(function (resolve, reject) {
if (timeout) { if (timeout) {
timer = setTimeout(function () { timer = setTimeout(function () {
@ -67,8 +75,7 @@ function jsonp(url, options) {
resolve(data); resolve(data);
}; };
params[callback] = id; params[callback] = id;
url += serialize(params); url = handleUrl(url, params);
url = url.replace('?&', '?');
// Create script. // Create script.
script = document.createElement('script'); script = document.createElement('script');
script.src = url; script.src = url;

View File

@ -1,6 +1,6 @@
{ {
"name": "simple-jsonp-promise", "name": "simple-jsonp-promise",
"version": "1.0.6", "version": "1.0.7",
"description": "", "description": "",
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {

View File

@ -14,63 +14,70 @@ let count = 0;
* - timeout {Number} how long after the request until a timeout error * - timeout {Number} how long after the request until a timeout error
* is emitted (defaults to `15000`) * is emitted (defaults to `15000`)
*/ */
function jsonp( url, options ) { function jsonp(url, options) {
options = options || {}; options = options || {};
let prefix = options.prefix || '__jp'; let prefix = options.prefix || '__jp';
let callback = options.callback || 'callback'; let callback = options.callback || 'callback';
let params = options.data || {}; let params = options.data || {};
let timeout = options.timeout ? options.timeout : 15000; let timeout = options.timeout ? options.timeout : 15000;
let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head; let target = document.getElementsByTagName('script')[0] || document.head;
let script; let script;
let timer; let timer;
let promise; let promise;
// Generate a unique id for the request. // Generate a unique id for the request.
let id = prefix + (count++); let id = prefix + (count++);
function noop() {} function noop() {}
function cleanup() { function cleanup() {
// Remove the script tag. // Remove the script tag.
if ( script && script.parentNode ) { if (script && script.parentNode) {
script.parentNode.removeChild( script ); script.parentNode.removeChild(script);
}
window[id] = noop;
if (timer) {
clearTimeout(timer);
} }
window[ id ] = noop;
if ( timer ) { clearTimeout( timer ); }
} }
function serialize( params ) { function serialize(params) {
let param = '?'; let param = '';
for ( let key in params ) { for (let key in params) {
if ( params.hasOwnProperty( key ) ) { if (params.hasOwnProperty(key)) {
param += `&${key}=${encodeURIComponent( params[ key ] )}`; param += `&${key}=${encodeURIComponent( params[ key ] )}`;
} }
} }
return param; return param;
} }
promise = new Promise( ( resolve, reject ) => { function handleUrl(url, params) {
if ( timeout ) { if (!~url.indexOf('?')) { url += '?'; }
timer = setTimeout( () => { url += serialize(params);
url = url.replace('?&', '?');
}
promise = new Promise((resolve, reject) => {
if (timeout) {
timer = setTimeout(() => {
cleanup(); cleanup();
reject( new Error( 'Timeout' ) ); reject(new Error('Timeout'));
}, timeout ); }, timeout);
} }
window[ id ] = function( data ) { window[id] = function (data) {
cleanup(); cleanup();
resolve( data ); resolve(data);
}; };
params[ callback ] = id; params[callback] = id;
url += serialize( params ); url = handleUrl(url, params);
url = url.replace( '?&', '?' );
// Create script. // Create script.
script = document.createElement( 'script' ); script = document.createElement('script');
script.src = url; script.src = url;
script.onerror = function() { script.onerror = function () {
cleanup(); cleanup();
reject( new Error( 'Network Error' ) ); reject(new Error('Network Error'));
}; };
target.parentNode.insertBefore( script, target ); target.parentNode.insertBefore(script, target);
} ); });
return promise; return promise;
} }