I am writing a DEMO today, where $. after the getJSON method is completed, it is found that jQuery is used in the entire DEMO, but it is not cost-effective to introduce a jQuery method. Therefore, a simple version is implemented by yourself, the basic requirements can be met:
Copy codeThe Code is as follows:
Var $ = {
GetJSON: function (url, params, callbackFuncName, callback ){
Var paramsUrl = "",
Jsonp = this. getQueryString (url) [callbackFuncName];
For (var key in params ){
ParamsUrl + = "&" + key + "=" + encodeURIComponent (params [key]);
}
Url + = paramsUrl;
Window [jsonp] = function (data ){
Window [jsonp] = undefined;
Try {
Delete window [jsonp];
} Catch (e ){}
If (head ){
Head. removeChild (script );
}
Callback (data );
};
Var head = document. getElementsByTagName ('head') [0];
Var script = document. createElement ('script ');
Script. charset = "UTF-8 ";
Script. src = url;
Head. appendChild (script );
Return true;
},
GetQueryString: function (url ){
Var result = {}, queryString = (url & url. indexOf ("? ")! =-1 & url. split ("? ") [1]) | location. search. substring (1 ),
Re =/([^ & =] +) = ([^ &] *)/g, m;
While (m = re.exe c (queryString )){
Result [decodeURIComponent (m [1])] = decodeURIComponent (m [2]);
}
Return result;
}
};
The DEMO is called as follows:
Copy codeThe Code is as follows:
Var url = "http://xxx.xxx.xxx? Callback = jsonp123 ";
Var params = {
A: 1,
B: 2
};
$. GetJSON (url, params, "callback", function (data ){
// Todo
});