jsonp原理

jsonp的原理本質上是利用script標籤加載了一段可執行的JavaScritp代碼json

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.parentNode.removeChild(script);
           }

       }
       script.src=url;
       document.body.appendChild(script);
   }
getJSONP.counter=0;
相關文章
相關標籤/搜索