JSONP Format Interface Implementation

Source: Internet
Author: User
Tags script tag

The unit has developed an app system, app outsourcing, and services provided by a self-developed payroll enquiry System. The app interacts with the backend to solve cross-domain problems with JSONP.

JSONP implementation, the need to add a callback,jsonp and ordinary JSON is the difference between ordinary JSON, when returned

Out.write ("{name: ' Xie Feng '}");

And the return of JSONP is

Out.write ("Callback ({name: ' Xie Feng '}");

Callback is actually a JS-side function name, the parties can arbitrarily agree

So the only difference for the server side is that the returned string is one more callback (XXXX)

    • JSONP can only use GET requests to resolve the same origin problem and return JavaScript code, because the request JavaScript file is not the same origin problem.

    • When the request data type is JSONP, Callback=jsonpcallback is added to the URL, http://localhost:8090/api/testcallback=jsonpCallback

    • The Jsonpcallback function is defined in the foreground JavaScript, which must be defined under window, which is the global function, otherwise it cannot be found.

    • The background gets the callback parameter value of the request Jsonpcallback, which returns the string "Jsonpcallback (Result)", resulting in the return result.

    • The request returns a script tag that calls the Jsonpcallback function first, and the success function is called, regardless of whether the function is found.

    • If you do not define JSONP and JSONPCALLBACK,JSONP default to "callback", Jsonpcallback will be the function name that jquery automatically generates.


You can refer to examples.

The code is as follows, struts configuration:

<action name= "Querysalarybykeyforapp" class= "salaryaction" method= "Querysalarybykeyforapp" ><result name= " Success ">jsonp.jsp</result></action>

jsonp.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><% String jsoncallback = Request.getparameter (" callback "); String jsonresult = (string) request.getattribute ("Salaryresult"); Out.println ("Callback (" +jsonresult+ ")");%>

Method implementation

Public string querysalarybykeyforapp ()  {try{saluserentity saluserquery = new  saluserentity (); if (salaryvo == null | |  salaryvo.getsalaryentity ()  == null) {salaryresult =  "{errormsg :  error}"; return success;} String staffid = salaryvo.getsalaryentity (). Getstaffid (); String year = salaryvo.getsalaryentity (). GetYear (); String month = salaryvo.getsalaryentity (). GetMonth (); String password = salaryvo.getsalaryentity (). GetPassword (); if (Stringutils.isempty (staffId)  | |  stringutils.isempty (year)  | |  stringutils.isempty (month)  | |  stringutils.isempty (password)) {salaryresult =  "{errormsg :  error}"; return success;} Saluserquery.setstaffid (StaffID), Saluserquery.setpassword (password), if (Saluserservice.valuser (salUserQuery)) { Saluserservice.valuser (Saluserquery); List<salaryentity> entities = salAryservice.querysalarylist (Salaryvo.getsalaryentity ()); if (entities == null | |  entities.size ()  != 1) {throw new salaryexception ("Your payroll data not entered, new contact hr! ");} List<map<string, object>> valuemap = entity2listofmap (entities.get (0)); Jsonarray jsonobject = jsonarray.fromobject (VALUEMAP);salaryresult =  Jsonobject.tostring ();//jsonobject.accumulate ("Jsonobject", Entities.get (0));//salaryvo.setsalaryentity ( Entities.get (0)); return success;}  else {salaryResult =  "{errormsg :  Error}"; return success;}} catch  (exception e)  {salaryResult =  "{errormsg :  error}"; return success;} Return error;}

JSON transformation:

Private jsonarray entity2listofmap (salaryentity salary)  {if (salary == null) { Salary = new salaryentity (); Return entity2listofmap (salary);} Jsonarray result  = new jsonarray (); Jsonobject basicinfo = new jsonobject ();//Basic Information jsonobject wageinfo = new  jsonobject ();//Wages Jsonobject buckleinfo = new jsonobject ();//real deduction of wages jsonobject  Realinfo = new jsonobject ();//real wage jsonobject gscdinfo = new jsonobject ();// The Company undertakes Jsonarray basicmap = new jsonarray (); Jsonarray wagemap = new jsonarray (); Jsonarray bucklemap = new jsonarray (); Jsonarray realmap = new jsonarray (); Jsonarray gscdmap = new jsonarray (); Jsonobject jsonobjectgh = new jsonobject (); Jsonobjectgh.put ("name",  "work number"); Jsonobjectgh.put ("Value", Salary.getstaffid()); Basicmap.add (JSONOBJECTGH); Jsonobject jsonobjectxm = new jsonobject (); Jsonobjectxm.put ("name",  "name"); Jsonobjectxm.put ("Value", Salary.getstaffname ()); Basicmap.add (JSONOBJECTXM); Jsonobject jsonobjectbm = new jsonobject (); Jsonobjectbm.put ("Name",  "department"); Jsonobjectbm.put ("Value", salary.getdept ()); Basicmap.add (JSONOBJECTBM); Jsonobject jsonobjectks = new jsonobject (); Jsonobjectks.put ("Name",  "department"); Jsonobjectks.put ("Value", Salary.getclass_ ()); Basicmap.add (JSONOBJECTKS); Jsonobject jsonobjectnf = new jsonobject (); Jsonobjectnf.put ("Name",  "year"); Jsonobjectnf.put ("Value", salary.getyear ()); Basicmap.add (JSONOBJECTNF); Jsonobject jsonobjectyf = new jsonobject (); Jsonobjectyf.put ("Name",  "month"); Jsonobjectyf.put ("Value", Salary.getmonth ()); Basicmap.add (JSONOBJECTYF); Basicinfo.put ("category",  "basic Information"); Basicinfo.put ("id",  "info"); Basicinfo.put ("Items",  basicmap); result. Add (basicinfo); return result;} 

The interface is provided in HTTP form:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/59/wKiom1bmfKiTyD_nAAIcMLZeHOM855.jpg "title=" f8f93ee1@ac8d246 (03-14-16-38-30). jpg "alt=" wkiom1bmfkityd_naaicmlzehom855.jpg "/>


This article is from the "Jiangnan Miner Technology Space" blog, please be sure to keep this source http://jncumter.blog.51cto.com/812546/1750964

JSONP Format Interface Implementation

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.