Home > Web Front-end > JS Tutorial > body text

Share a piece of code that jQuery Ajax requests background data and receives in the foreground

怪我咯
Release: 2017-03-29 15:16:34
Original
1768 people have browsed it

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的格式
},
});
Copy after login


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();
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!