In the recent user comments on the portal, a long time did not use jquery just use, did not expect to use the Post method to deal with Ajax callback JSON data, anyway, the background is a JSON returned. Unexpectedly so small a problem hang me for several hours, then I Ajax method processing, incredibly OK, a comparison found that the original post method callback JSON must eval, and Ajax methods do the default processing. I hope you will be careful.
function haha () {
jquery.post ("addcomment!comment.action",
function AA (data) {
data = eval (data);// The Post method must be added, and the Ajax method automatically handles
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);}}
);
Background:
Copy Code code as follows:
Public String Comment () {
try{
User U = New User ("User", "Koko");
List = new arraylist<user> ();
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 code as follows:
<package name= "Ajax" extends= "Json-default" >
<action name= "AddComment" class= "Org.test.action.CommentAction" >
<result type= "JSON" >
<param name= "Root" >list</param>
</result>
</action>