For Jsonp and cross-domain issues, please search by yourself.
This paper focuses on the simulation implementation code of AJAX JSONP, the basic principle of JSONP in code is also at a glance.
<HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <title>Analog Ajax JSONP Source code implementation</title> <Scripttype= "Text/javascript"> //Generate random string functionRandomString (len) {len=Len|| +; varchars= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnoprstuvwxyz1234567890"; varMaxpos=chars.length; varpwd= "'; for(i= 0; I<Len; I++) {pwd+=Chars.charat (Math.floor (Math.random ()*maxpos)); } returnpwd; } functionAjax_jsonp (data) {//generate a random function name and point to the incoming callback function varCallbackfun= "Jquery_" +RandomString ( +); Eval (Callbackfun+ "= data.success;"); //provide the URL address of the JSONP service (regardless of the type of address, the resulting return value is a piece of JavaScript code) varURL=Data.url+ "&callback="+Callbackfun; //Create a script tag, set its properties varScript=Document.createelement ('Script'); Script.setattribute ('src', URL); //Add the script tag to head, and the call startsdocument.getElementsByTagName ('Head')[0].appendchild (script); } //Impersonation Callajax_jsonp ({URL:"http://sax.sina.com.cn/newimpress?adunitid=PDPS000000047325&rotate_count=36", Success:function(data) {alert ('ID' +data.ad[0].id); } }); </Script></Head><Body></Body></HTML>
Basic Work Ideas:
1. Create a random string as the name of the callback function
2. Assign the incoming callback function to the random function.
3. Add the random function name as the callback parameter to the incoming URL (the cross-domain URL that you really want to access)
4. Dynamically create a SCRIPT element and trigger access to the URL. The browser gets the JS code that is returned and executes the incoming callback function.
(The third-party URL returned must be a valid JS function call string, where the function name is the parameter callback, the parameter is the data that is really to be returned.) )
Reference articles
Note: This article belongs to the original blogger, please indicate the original link, thank you for your cooperation.
Http://www.cnblogs.com/zhangfanwen/p/5486860.html
AJAX JSONP Source Code implementation