JSONP Super Simple example, a look can be started

Source: Internet
Author: User

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

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.