服務端代碼:主要是返回的時候,返回值要用callback包裝一下ajax
/** * JSONP調用 * * @param request * @return */ @RequestMapping("/remote/jsonp") public void remoteJsonp(HttpServletRequest request, HttpServletResponse response) throws IOException { String jsonpCallback = request.getParameter("jsonpCallback"); String data=request.getParameter("data"); //todo something ActionResultEntity result = new ActionResultEntity(); //設置返回值 String returnValue = jsonpCallback + "(" + StringUtil.toJsonString(result) + ")"; response.getWriter().write(returnValue); }
js調用代碼:json
$.ajax({ async: false, type: "post", url:"http://localhost:8080/main/remote/jsonp", data: { data: "test" }, dataType: "jsonp", jsonp: "jsonpCallback", success: function (successJson) { }, error: function (errorJson,text,message) { } });