JSONP Client-specific implementations
1. Remotely invoke a JS, the code is as follows:
<script type= "Text/javascript" >
var Localhandler = function (data) {
Alert (' I am a local function, can be called cross-domain remote.js file, remote JS brings the data is: ' + data.result);
};
</script>
<script type= "Text/javascript" src= "Http://remoteserver.com/remote.js" ></script>
The Remote.js file code is as follows:
Localhandler ({"Result": "I am the data brought by remote JS"});
2. Dynamically generated Scripts
<script type= "Text/javascript" >
callback function after getting the flight information query result
var Flighthandler = function (data) {
Alert (' Your flight results are: fare ' + Data.price + ' yuan, ' + ' + ' + data.tickets + ' Zhang. ‘);
};
Provide the URL address of the JSONP service (regardless of the type of address, the resulting return value is a piece of JavaScript code)
var url = "Http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998&callback=flightHandler";
Create a script tag, set its properties
var script = document.createelement (' script ');
Script.setattribute (' src ', url);
Add the script tag to head, and the call starts
document.getElementsByTagName (' head ') [0].appendchild (script);
</script>
Flighthandler ({
"Code": "CA1998",
"Price": 1780,
"Tickets": 5
});
3. Encapsulating Code
Encapsulate the code to facilitate interaction with the user interface, enabling multiple and repeated calls.
JSONP Client-specific implementations