jQuery Ajax请求后台数据并在前台接收

原创 2016-12-26 15:47:46 254
摘要:这篇文章主要介绍了jQuery Ajax请求后台数据并在前台接收的相关资料。1、前台使用jQuery ajax请求$.ajax({ url: "r_getRolePer.action", dataType:'json', data: {userId:"1"}, //请求的附加参数,用json对象 method:&

这篇文章主要介绍了jQuery Ajax请求后台数据并在前台接收的相关资料。

1、前台使用jQuery ajax请求

$.ajax({
url: "r_getRolePer.action",
dataType:'json',
data: {userId:"1"}, //请求的附加参数,用json对象
method:'POST',
success: function(data){
$.messager.alert('消息',data.add,'');
//这里使用的时easyui的格式
},
});

   

2、在action里面使用response.getWriter()的PrintWriter对象将数据打印到前台

public PrintWriter out()throws IOException{
HttpServletResponse response=ServletActionContext.getResponse();
    response.setContentType("text/html");
    response.setContentType("text/plain; charset=utf-8");
    PrintWriter out= response.getWriter();
    return out;
}

*******

JSONObject permision = new JSONObject();

permision.put("add", 0);

permision.put("DELETE", 0);

permision.put("UPDATE", 0);

out().print(permision.toString());

out().flush();

out().close();

   

注意这里将permision对象打印到前台是一个json字符串,在第一步的ajax请求的success返回的data取到的就是这个permision对象,所以使用data.add就个可以取到json对象的值,如果后台打印到前台的数据不是json的话而是list对象使用data[0]这样也可以取到,但是推荐son传递。

更多关于jQuery Ajax请求后台数据并在前台接收请关注PHP中文网(www.php.cn)其它文章! 

发布手记

热门词条