-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import inBrowser from './inBrowser' | ||
import isObject from './isObject' | ||
import noop from './noop' | ||
import randomString from './randomString' | ||
import reduce from './reduce' | ||
|
||
const objectToQueryString = (obj: object): string => { | ||
return reduce( | ||
obj, | ||
(str, value, key) => str += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`, | ||
'' | ||
) | ||
} | ||
|
||
export default function jsonp(url: string, data?: string | object, callbackParameter = 'callback'): Promise<any> { | ||
return new Promise( | ||
inBrowser() | ||
? (resolve, reject) => { | ||
let script: HTMLScriptElement | ||
|
||
const callbackFunctionName = `jsonp${randomString()}` | ||
const queryString = `${ | ||
isObject(data) ? objectToQueryString(data) : (data != null ? String(data) : '') | ||
}&${ | ||
encodeURIComponent(callbackParameter) | ||
}=${ | ||
encodeURIComponent(callbackFunctionName) | ||
}` | ||
const requestUrl = url + (url.indexOf('?') > -1 ? '&' : '?') + queryString; | ||
|
||
(window as any)[callbackFunctionName] = (response: any) => { | ||
resolve(response) | ||
try { | ||
delete (window as any)[callbackFunctionName] | ||
document.body.removeChild(script) | ||
} catch (err) {} | ||
} | ||
|
||
script = document.createElement('script') | ||
script.src = requestUrl | ||
script.async = true | ||
script.onerror = reject | ||
|
||
document.body.appendChild(script) | ||
} | ||
: noop | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* 返回一个随机的字符串。 | ||
* | ||
* @returns 随机的字符串 | ||
*/ | ||
export default function randomString(): string { | ||
return Math.random().toString(36).substr(2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters