[fix] params serialize
This commit is contained in:
parent
2ae82c15f8
commit
9b480dd19f
3 changed files with 50 additions and 36 deletions
|
@ -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;
|
||||||
|
|
|
@ -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": {
|
||||||
|
|
15
src/index.js
15
src/index.js
|
@ -35,11 +35,13 @@ function jsonp( url, options ) {
|
||||||
script.parentNode.removeChild(script);
|
script.parentNode.removeChild(script);
|
||||||
}
|
}
|
||||||
window[id] = noop;
|
window[id] = noop;
|
||||||
if ( timer ) { clearTimeout( timer ); }
|
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 ] )}`;
|
||||||
|
@ -48,6 +50,12 @@ 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((resolve, reject) => {
|
promise = new Promise((resolve, reject) => {
|
||||||
if (timeout) {
|
if (timeout) {
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
|
@ -60,8 +68,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;
|
||||||
|
|
Loading…
Add table
Reference in a new issue