1. Use jQuery ajax request at the front desk
$.ajax({ url: "r_getRolePer.action", dataType:'json', data: {userId:"1"}, //请求的附加参数,用json对象 method:'POST', success: function(data){ $.messager.alert('消息',data.add,''); //这里使用的时easyui的格式 }, });
2. Use the printWriter object of response.getWriter() in the action to print the data to the front desk.
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();
Note that the permision object printed to the front desk is a jsonstring. In the first step, the data returned by the success of the ajax request is obtained. It is this permision object, so you can use data.add to get the value of the json object. If the data printed from the background to the front is not json, but the list object, you can also use data[0] to get it. to, but it is recommended to pass it by son
The above is the detailed content of Share a piece of code that jQuery Ajax requests background data and receives in the foreground. For more information, please follow other related articles on the PHP Chinese website!