java side code:
/** * AJAX cross-domain verification of user status * @param request * @param response * @throws IOException */ @RequestMapping("ajaxCheckCross.html") public void doAjaxCheckCross(HttpServletRequest request, HttpServletResponse response) throws IOException { // User Info UserInfoVo user = (UserInfoVo) WebUtils.getSessionContainer(request).getUserInfo(); // callback parameter String callback = request.getParameter("callbackparam"); JSONObject json = new JSONObject(); if (user != null) { json.put("res", "login"); json.put("mobile", StringMarkUtil.markMobile(user.getAccount())); } else { json.put("res", "out"); } PrintWriter out = response.getWriter(); // output out.write(callback + "(" + json.toString() + ")"); }
js side:
$.ajax({ type : "get", async:false, url : "ajax.do", dataType : "jsonp", jsonp: "callbackparam",//The parameter name passed to the request handler or page to get the name of the jsonp callback function (default: callback) jsonpCallback:"success_jsonpCallback",//custom jsonp callback function name, the default is the random function name automatically generated by jQuery success : function(json){ alert(json); alert(json[0].name); }, error:function(){ alert('fail'); } });