Home  >  Article  >  Web Front-end  >  Comparison of different examples of ajax and post processing json in jQuery_jquery

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

WBOY
WBOYOriginal
2016-05-16 16:32:201202browse

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);
}
});
}

Backstage:

Copy code The code is as follows:

public String comment() {
try{
User u = new User("user", "koko");
list = new ArrayList4c8e0c17c3bd7e0081bb17cc795e1984();
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:

2678acd061d93e3b616b00474912c4fa
b14aadd1f0c5390d75d10388858e6400
6c3a65a197edb72c1848318f5c8eec68
3304a7319e3272402abe8d15d3e9329clist8bb7487ae6a16a43571bc14c7fcf93c2
6f55ec50a52710d38f3a2f7e427abe79
bd366a3fff9c30beec99faf698cf52ce

Statement:
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