the principle of Jsonp
The most basic principle of JSONP is: Add a <script> tag dynamically, and the SRC attribute of the script tag is not a cross-domain limitation.
<script type= "Text/javascript" >
function Jsonpcallback (Result) {
alert (result.msg);
}
</script>
<script type= "Text/javascript" src= "Http://crossdomain.com/jsonServerResponse?jsonp=jsonpCallback" ></ Script>
Where Jsoncallback is a function that is registered by the client and gets the JSON data on the cross-domain server after the callback.
Http://crossdomain.com/jsonServerResponse?jsonp=jsonpCallback
This URL is the interface of the cross-domain server to fetch JSON data, the parameter is the name of the callback function, the return format is:
Jsonpcallback ({msg: ' This is JSON data '})
1. Register a callback on the client and pass the callback name to the server. At this point, the server becomes JSON data. Then, in JavaScript syntax, a function is generated, and the function name is the parameter Jsonp passed up.
2. Put the JSON data directly into the function in the form of a parameter, so that a section of the JS syntax is generated and returned to the client.
3. The client browser, parses the script tag and executes the returned JavaScript document, where the data is passed in as a parameter to the client's pre-defined callback function. (Dynamic execution callback function).
Simply put: After the client declares the callback callback function, the client requests data across the domain through the script tag, then the server returns the appropriate JSON data and executes the callback function dynamically.
Defining AJAX functions
function Ajaxfun () {
var timeStamp = Math.floor (new Date (). GetTime ()/1000);
var url = "Http://apiso.alidemo.3gpp.cn/httpserver/cp/yisou/ali_feedback_interface.php?callback=jsonpCallback &feedbacktype=add&type= "+ feedbacknumber +" &book= "+ me.mixedInfo.title +" &author= "+ Me.mixedInfo.author + "&chapter=" + me.mixedInfo.cname + "&chapterid=" + me.mixedInfo.cid + "&questiondesc=" + text + "&platform=1&t=" + TimeStamp + "&sn=" + MD5 ("d30fcd1a9f1900fa049b4766e0a275e1" + timeStamp);
var scriptobj = document.createelement ("script");
scriptobj.src = URL;
Scriptobj.id = "Jsonpscript";
Document.body.appendChild (Scriptobj);
JSONP callback function, Jsonpcallback must be a global function, because JSONP returns the statement that executes the function in the global environment, that is, jsonpcallback (data)
Window.jsonpcallback = function (data) {
Switch (data.code) {
Case "1":
Novel.readerprompt (' Submit successful, coming back ... ', 1, function () {
Window.history.go (-1);
});
Break
Case "0":
Novel.readerprompt (' Commit failed. ', 2);
Break
Case "900":
Novel.readerprompt (' Commit failed, validation failed. ', 2);
Break
}
After successful deletion of Scriptobj, the latter settimeout will not be executed.
if (document.getElementById ("Jsonpscript")) {
Document.body.removeChild (Scriptobj);
}
}
Set time-out, time-out, direct display of successful commit
SetTimeout (function () {
if (document.getElementById ("Jsonpscript")) {
Document.body.removeChild (Scriptobj);
Novel.readerprompt (' Submit successful, coming back ... ', 1, function () {
Window.history.go (-1);
});
}
}, 2000);
}
Ajaxfun ();
The principle of JSONP