The difference between ajax and post processing json in jQuery is that jqueryjson

Source: Internet
Author: User

The difference between ajax and post processing json in jQuery is that jqueryjson

Recently, when I was making a portal user comment, I had not used jquery for a long time. I didn't expect to steal the post method to process json data of ajax callback, and I couldn't get it, json is returned in the background. Unexpectedly, I had to wait for several hours for such a small problem. Then I processed the ajax method and found that the original post method callback json must be eval, the ajax method does the default processing. Please be careful...

 

Function haha () {jQuery. post ("addComment! Comment. action ", function aa (data) {data = eval (data); // the POST method is required. The ajax method automatically processes 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:

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:

<package name="ajax" extends="json-default"><action name="addComment" class="org.test.action.CommentAction"><result type="json">   <param name="root">list</param></result></action>。。。。。。

 


When the jquery post method is used for asynchronous access, the returned json data cannot be parsed?

1. If you specify datatype: 'json', it will not work if the background is not converted to json format.
2. parse is used to parse json objects from a string.
3. If the background returns an object in json format, you can directly access the object through the data point attribute.
If the backend is list to josn, you can use the data [0] Point attribute. Of course, your ype is json.

Q: How can I deal with the AJAX connection duration of JSON data returned by jQueryPOST?

$. Post is a simple method for encapsulating ajax post requests, so you cannot perform more detailed configuration.
Use $. ajax to set the timeout value.
For example
$. Ajax ({
Url: "xxx. php ",
Type: "POST ",
DataType: "json ",
Timeout: 60000, // timeout time, which is now set to 60 s.
Success: function (data ){
Console. log (data );
}
});

For more detailed configuration, see the jQuery API documentation.

In addition, the timeout time configured on your server must be no less than that configured in jQuery. Otherwise, jQuery has not timed out, and the server directly returns timeout.

At the same time, it is also a good choice to optimize the processing of the server and increase the corresponding speed.

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.