$.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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号