/** * test * @param * @return */ @RequestMapping("/test") @ResponseBody public void test(HttpServletRequest request, HttpServletResponse response)throws Exception{ response.setContentType("text/plain"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Access-Control-Allow-Origin", "*");//添加跨域访问 Map<String, Object> result = new HashMap<String, Object>(); Map<String,Object> data=new HashMap<String,Object>(); String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数 try { String test=request.getParameter("test"); result.put("errorCode", "0"); result.put("errorMessage","success!"); result.put("data",data); } catch (IOException e) { e.printStackTrace(); }finally { PrintWriter out = response.getWriter(); JSONObject resultJSON = JSONObject.fromObject(result); //根据需要拼装json out.println(jsonpCallback+"("+resultJSON.toString(1,1)+")");//返回jsonp格式数据 out.flush(); out.close(); } }
java jsonp接口
最新推荐文章于 2024-09-20 10:26:14 发布