基于promise和script标签的jsonp
2021-07-02 21:06
标签:ons window cti creat element document url back win 基于promise和script标签的jsonp 标签:ons window cti creat element document url back win 原文地址:http://www.cnblogs.com/liuhao-web/p/7126945.htmlfunction Jsonp(url){
var url=url.indexOf(‘?‘)>-1?url+"&callback=callback":url+"?callback=callback";
return new Promise(function(resolve,reject){
var script=document.createElement(‘script‘);
script.src=url
document.head.appendChild(script);
window.callback=function(param){
resolve(param)
}
})
}
//后台返回数据示例:callback({‘name‘:‘liuhao‘})
Jsonp("http://192.168.1.100:2015/").then(function(res){
console.log(res)//{‘name‘:‘liuhao‘}
})