Different examples of Ajax and post processing JSON in jquery compare _jquery

Source: Internet
Author: User
Tags eval

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.