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');
}
});
标签:跨域,request,json,ajax,user,jsonp,put,out From: https://blog.51cto.com/u_16240770/7278970