Method of sending an JSONP request using the Script element
- 2021-06-28 10:19:52
- OfStack
Method of sending an JSONP request using the Script element
// As specified URL Send out 1 individual JSONP request
// The corresponding parsed data is then passed to the callback function
// stay URL Add in 1 Named jsonp Query parameters for , The name of the callback function used to specify the request
function getJSONP(url, callback){
// Create for this request 1 Only 1 Callback function name
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;