JSON (JavaScript Object Notation) and Jsonp (JSON with Padding) have only one letter difference, but they're not the same thing at all: JSON is a data interchange format, And Jsonp is a kind of unofficial cross-domain data interaction protocol that is created by the ingenuity of the developers. JSONP solves the problem of Ajax cross-domain requests, and JSONP only resolves one of the cross-domain request scenarios.
The following is a simple introduction to the JSONP interface development process based on the servlet:
1, create a servlet interface Jsonpservlet
class jsonpservlet extends httpservlet { Doget (HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RESP) servletexception, ioexception { //callback function name String callback = req.getparameter (); Resp.setcharacterencoding (); jsonobject jo = Jsonobject (); jo.put (,); system. println (Jo.tojsonstring ()); system. println (Jo.tostring ()); //wraps the JSON into the function name resp.getwriter (). Print (Callback + + jo.tojsonstring () + ); } &nBsp; dopost (HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RESP) ServletException, Ioexception { doget (REQ, RESP); }} |
2. Create an HTML file and call the Jsonp interface
& nbsp; var Localhandler = function (data) { alert (' i am local fun,can be called by remote.js,remote js return data : ' + data.result); }; var url = "http://localhost/jsonpServlet?callback= Localhandler "; var script = Document.createelement (' script '); script.setattribute (' src '), url); document.getelementsbytagname (' head ') [0]. AppendChild (script); |
After the call succeeds, the browser pops up the dialog box
| 650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8E/25/wKiom1i2lPWT3iZeAADnq1X2H64226.png-wh_500x0-wm_ 3-wmp_4-s_2505401262.png "title=" 00.png "alt=" Wkiom1i2lpwt3izeaadnq1x2h64226.png-wh_50 "/> |
This article is from the "Big Data Practitioner" blog, please contact the author!
JSONP Super Simple example, a look can be started