AJAX JSONP Source Code implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.