原理
实际上是利用script没有跨域限制,先创建一个script标签,src里面放服务端请求路径,参数后面会带着callback=xxx,这个实际是个回调,返回的数据会是:xxx(json)这样的格式。。
JsonP插件
地址:https://www.npmjs.com/package/jsonp
github:https://github.com/webmodules/jsonp
install:
npm install jsonp
API:
jsonp(url, opts, fn)
url (String) url to fetch
opts (Object), optional
-param (String) name of the query string parameter to specify the callback (defaults to callback) –
Param 重点关注
-timeout (Number) how long after a timeout error is emitted. 0 to disable (defaults to 60000)
-prefix (String) prefix for the global call