jQuery中ajax和post处理json的不同
1个回答
展开全部
注意如果post获取,则要将返回的数据eval()一下,否则取不到数据;
function haha() {
jQuery.post("addComment!comment.action",
function aa(data) {
data = eval_r(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);
}
});
}
后台:
[java] view plaincopyprint?
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;
}
配置:
[html] view plaincopyprint?
<</SPAN>package name="ajax" extends="json-default">
<</SPAN>action name="addComment" class="org.test.action.CommentAction">
<</SPAN>result type="json">
<</SPAN>param name="root">list</</SPAN>param>
</</SPAN>result>
</</SPAN>action>
。。。。。。
function haha() {
jQuery.post("addComment!comment.action",
function aa(data) {
data = eval_r(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);
}
});
}
后台:
[java] view plaincopyprint?
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;
}
配置:
[html] view plaincopyprint?
<</SPAN>package name="ajax" extends="json-default">
<</SPAN>action name="addComment" class="org.test.action.CommentAction">
<</SPAN>result type="json">
<</SPAN>param name="root">list</</SPAN>param>
</</SPAN>result>
</</SPAN>action>
。。。。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询