ajax跨域jsonp
java端代码:
/** * AJAX跨域检证用户状态 * @param request * @param response * @throws IOException */ @RequestMapping("ajaxCheckCross.html") public void doAjaxCheckCross(HttpServletRequest request, HttpServletResponse response) throws IOException { // 用户信息 UserInfoVo user = (UserInfoVo) WebUtils.getSessionContainer(request).getUserInfo(); // 回调参数 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(); // 输出 out.write(callback + "(" + json.toString() + ")"); }
js端:
$.ajax({ type : "get", async:false, url : "ajax.do", dataType : "jsonp", jsonp: "callbackparam",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback:"success_jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 success : function(json){ alert(json); alert(json[0].name); }, error:function(){ alert('fail'); } });
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05