使用Script元素发送JSONP请求的方法
使用Script元素发送JSONP请求的方法
// 根据指定URL发送一个JSONP请求 //然后把解析得到的相应数据传递给回调函数 //在URL中添加一个名为jsonp的查询参数,用于指定该请求的回调函数的名称 function getJSONP(url, callback){ //为本次请求创建一个唯一的回调函数名称 var cbnum = "cb"+getJSONP.counter++; var cbname = "getJSONP."+cbnum; if(url.indexof("?") === -1){ url += "?jsonp="+cbname; }else { url += "&jsonp="+cbname; } var script = document.createElement("script"); getJSONP[cbnum] = function(response){ try{ callback(response); }finally{ delete getJSONP[cbnum]; script.prentNode.removeChild(script); } }; script.src = url; document.body.appendChild(script); } getJSONP.counter = 0;
相关推荐
zmosquito 2020-05-10
somebodyoneday 2020-04-22
fengchao000 2020-04-22
fengchao000 2020-04-11
fengchao000 2020-03-05
xiaouncle 2020-02-13
somebodyoneday 2020-03-06
fengchao000 2020-06-17
adonislu 2020-05-16
adonislu 2020-05-10
Richardxx 2020-03-07
somebodyoneday 2020-02-16
baijinswpu 2020-01-29
fengchao000 2020-01-10
fengchao000 2019-12-25
newthon 2019-12-23
somebodyoneday 2013-07-11