JS part
(Function (window, document) {
Varjsonp =function (URL, data, callback) {
1. Mount callback function
Varfnsuffix = Math.random (). toString (). replace ('. ', ');
Varcbfuncname = ' MY_JSON_CB ' + fnsuffix;
Window[cbfuncname] = callback;
2, the form of converting data into a URL string
{id=1,count=4}==>id=1&count=4
varquerystring = Url.indexof ('? ') = = 1? '? ': ' & '; Determine if there is a last in the URL? , no then?
QueryString + = key + ' = ' + Data[key] + ' & ';
3, processing URL in the callback function Url+=callback=sdgade
QueryString + = ' callback= ' + cbfuncname;
Querystring=?id=1&count=4&callback=sdgade
Varscriptelement = document.createelement (' script ');
SCRIPTELEMENT.SRC = URL + querystring;
5, put the script tag on the page
Document.body.appendChild (scriptelement);
|
Testing in the page
<script type= "Text/javascript" src= "Http.js" ></script>
$jsonp (' Http://api.douban.com/v2/movie/in_theaters ', {},
document.getElementById (' htt '). innerhtml=json.stringify (data);
|
Results can return the results, the page is displayed as, indicating success!