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

Comparison of different examples of ajax and post processing json in jQuery_jquery

WBOY
Release: 2016-05-16 16:32:20
Original
1228 people have browsed it

Recently, when I was making user comments on the portal, I hadn’t used jquery for a long time and I just happened to use it. I didn’t expect to cut corners and use the post method to process the json data of the ajax callback. I couldn’t get it, and there was json returned in the background. . Unexpectedly, such a small problem hung me up for several hours. Later, I handled it with the ajax method and it was OK. After a comparison, I found that the post method callback json must be eval, and the ajax method has been processed by default. Please be careful.

function haha() {
jQuery.post("addComment!comment.action",
function aa(data) {
data = eval(data);//POST方法必加,ajax方法自动处理了
alert(data[0].userId);
alert(data[0].userName);
},
"json"
);

jQuery.ajax({
type:"post",
url:"addComment!comment.action",
dataType:"json",
success: function aa(data) {
alert(data[0].userId);
alert(data[0].userName);
}
});
}
Copy after login

Backstage:

Copy code The code is as follows:

public String comment() {
try{
User u = new User("user", "koko");
list = new ArrayList();
list.add(u);
//map.put("id", userId);
// JSONObject jb = JSONObject.fromObject(list); // name:" userName ",
// info = jb.toString();
System.out.println(list);
}
catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}

Configuration:

Copy code The code is as follows:




list



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