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请求后台数据并在前台接收,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

以上是 jQuery Ajax请求后台数据并在前台接收 的全部内容, 来源链接: utcz.com/z/329764.html

回到顶部