本文共 4605 字,大约阅读时间需要 15 分钟。
单位开发了一个app系统,app外包,服务由自开发的薪资查询系统提供。app与后端交互采用解决跨域问题。
JSONP的实现,需要加上一个callback,JSONP和普通JSON的区别在于普通JSON,返回时
out.write("{name:'Xie Feng'}");
而jsonp的返回则是
out.write("callback({name:'Xie Feng'}");
callback实际是一个js端的函数名称,双方可以任意约定
所以对于服务器端唯一区别在于,返回的string多一个callback(xxxx)
jsonp只能使用get请求,解决同源问题,返回javascript代码,因为请求javascript文件是没有同源问题的。
当请求数据类型为jsonp时,会将callback=jsonpCallback加在url上,http://localhost:8090/api/testcallback=jsonpCallback
前台javascript中定义jsonpCallback函数,此函数必须定义在window下,也就是全局的函数,否则找不到。
后台获取请求的callback参数值jsonpCallback,返回字符串"jsonpCallback(result)",result为返回结果。
请求返回的是script tag,首先会调用jsonpCallback函数,不管是否找到该函数,都会调用success函数。
如果没有定义jsonp和jsonpCallback,jsonp默认为"callback",jsonpCallback会是Jquery自动生成的函数名。
可以参考。
代码如下,struts配置:
1 2 3 | <action name= "querySalaryByKeyForApp" class = "salaryAction" method= "querySalaryByKeyForApp" > <result name= "success" >jsonp.jsp</result> </action> |
jsonp.jsp
1 2 3 4 5 6 7 8 | <%@ 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+")"); %> |
方法实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public String querySalaryByKeyForApp() { try { SalUserEntity salUserQuery = new SalUserEntity(); if (salaryVo == null || salaryVo.getSalaryEntity() == null ){ salaryResult = "{errorMsg : 错误}" ; 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 : 错误}" ; 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( "您的薪资数据未录入,新联系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 : 错误}" ; return SUCCESS; } } catch (Exception e) { salaryResult = "{errorMsg : 错误}" ; return SUCCESS; } //return ERROR; } |
json转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | private JSONArray entity2ListOfMap(SalaryEntity salary) { if (salary == null ){ salary = new SalaryEntity(); return entity2ListOfMap(salary); } JSONArray result = new JSONArray(); JSONObject basicInfo = new JSONObject(); //基本信息 JSONObject wageInfo = new JSONObject(); //应发工资 JSONObject buckleInfo = new JSONObject(); //实扣工资 JSONObject realInfo = new JSONObject(); //实发工资 JSONObject gscdInfo = new JSONObject(); //公司承担 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" , "工号" ); jsonObjectgh.put( "value" ,salary.getStaffId()); basicMap.add(jsonObjectgh); JSONObject jsonObjectxm = new JSONObject(); jsonObjectxm.put( "name" , "姓名" ); jsonObjectxm.put( "value" ,salary.getStaffName()); basicMap.add(jsonObjectxm); JSONObject jsonObjectbm = new JSONObject(); jsonObjectbm.put( "name" , "部门" ); jsonObjectbm.put( "value" ,salary.getDept()); basicMap.add(jsonObjectbm); JSONObject jsonObjectks = new JSONObject(); jsonObjectks.put( "name" , "科室" ); jsonObjectks.put( "value" ,salary.getClass_()); basicMap.add(jsonObjectks); JSONObject jsonObjectnf = new JSONObject(); jsonObjectnf.put( "name" , "年份" ); jsonObjectnf.put( "value" ,salary.getYear()); basicMap.add(jsonObjectnf); JSONObject jsonObjectyf = new JSONObject(); jsonObjectyf.put( "name" , "月份" ); jsonObjectyf.put( "value" ,salary.getMonth()); basicMap.add(jsonObjectyf); basicInfo.put( "category" , "基本信息" ); basicInfo.put( "id" , "info" ); basicInfo.put( "items" , basicMap); result.add(basicInfo); return result; } |
接口用http形式提供: