Before introducing jsonp, we will briefly introduce some JSON. JSON is short for JavaScript Object Notation and is a lightweight and readable open standard for text-based data exchange. It is derived from the presentation of simple data structures and associated arrays in javsscript programming language. It is a plain text that only contains data pairs and simple brackets. Therefore, JSON messages can be transmitted in many ways.
1. jsonp Definition
Jsonp, short for JSON with padding, is an unofficial protocol. It allows the server to generate a script tags to return to the client, and implement site access through javascript callback. Jsonp is a script tag injection that adds the response returned by the server to the page to implement specific functions.
2. Origin of jsonp
To explain the reason for jsonp, let's first talk about the browser's "same-origin policy (SOP: Same Origin Policy )". In short, the browser restricts that the script program can only interact with scripts of the same Protocol, same domain name, and same port, including sharing and passing variables. Cookie transmission follows the same policy. This causes some problems when integrating applications involving multiple servers. Cross-origin access causes Ajax code of Site A to be unable to access data of site B.
How can cross-origin access be solved? It requires the use of a browser feature: although the browser does not allow the script program on the page to read data across domains, it allows HTML to reference cross-origin resources, fragments, CSS, and script programs. The reference to the script program is special. After it is parsed by the browser, it is irrelevant to the local script program and can be interpreted and executed immediately. For example, in a JS file of Site B, a simple prompt box is: Alert ("This is Victor !");. When this JS is referenced at Site A, the script will be executed in Site B's application and an alert message will be displayed. Because the reference of an out-of-Site Script is implemented through the script tag, in addition, the script program can use Dom to control all tags on the HTML page (including dynamically creating SCRIPT tags ), this allows you to change local resources by calling an out-of-Site program. In addition, through the use of the <SCRIPT> tag, you can directly return executable JavaScript function calls or JSON data from the server.
3. jsonp Principle and Implementation
Register a callback on the client, and then pass the callback name to the server. In this case, the server is converted into JSON data. Then, a function is generated in Javascript syntax. The function name is the passed parameter jsonp.
Then, place the JSON data directly in the function as an input parameter. In this way, a JS syntax document is generated and returned to the client.
Finally, parse the script tag in the browser of the client and execute the returned JavaScript document. As a parameter, the data is passed into the pre-defined callback function of the client (dynamic execution of the callback function ).
In fact, jsonp is a very simple thing. The <script/> label is used to dynamically parse JavaScript documents. (You can also use the eval function ).
4. The following code is implemented using JS and jquery respectively:
JS Code in test. jsp:
JS:
<SCRIPT type = "text/JavaScript"> <br/> function jsonpcallback (result) {<br/> alert (result [1]. name); <br/>}< br/> </SCRIPT> <br/> <SCRIPT type = "text/JavaScript" src = "http: // localhost: 8080/jsonp. JSP? Callback = jsonpcallback "> </SCRIPT>
Or jquery code:
<SCRIPT type = "text/JavaScript"> <br/> $. getjson ("http: // localhost: 8080/jsonp. jsp? Callback =? ", Function (JSON) {<br/> alert (JSON [0]. Name); <br/>}); <br/> </SCRIPT>
Jsonp. JSP code:
<% <Br/> string callback = request. getparameter ("Callback"); <br/> out. print (callback + "([{Name: 'john', age: '19'}, {Name: 'job', age: '20'}]);"); <br/> out. print (callback); <br/>%>
Of course, the background can be implemented using action or servlet.
Finally, John is displayed as long as the browser inputs http: // localhost: 8080/jsonp/test. jsp.