Example of JSON-based ajax parameter passing using jQuery (with Json plug-in)

Source: Internet
Author: User
Tags tojson

JQuery's ajax call is very convenient. When passing parameters, you prefer the Json data format. For example:
Copy codeThe Code is as follows:
Function AddComment (content ){
Var threadId = $ ("# span_thread_id" ).html ();
Var groupId = $ ("# span_group_id" pai.html ();
Var groupType = $ ("# span_group_type" pai.html ();
Var title = $ ("# thread_title" pai.html ();
Var content = content. replace (/\ x22/g ,'"');
$. Ajax ({
Url: '/WebService/GroupService. asmx/AddThreadComment ',
Data: '{threadId:' + threadId + ', groupId:' + groupId + ', groupType:' + groupType + ', title: "' + title + '", content: "'+ content +'"} ', type: 'post ',
DataType: 'json ',
ContentType: 'application/json; charset = UTF-8 ',
Cache: false,
Success: function (data ){
// Determine whether the request is successful Based on the returned value data. d.
},
Error: function (xhr ){
// An exception occurs in the middle. View xhr. responseText
}
});
}

This is the most troublesome and error-prone process. The Json string is concatenated. The value of the parameters must be enclosed in quotation marks and special processing must be performed on ',/, and other text fields entered by the user.

In an unexpected opportunity, my boss recommended me a new method. Let's look at the following code:
Copy codeThe Code is as follows:
Function AddComment (content ){
Var comment = {};
Comment. threadId = $ ("# span_thread_id" pai.html ();
Comment. groupId = $ ("# span_group_id" pai.html ();
Comment. groupType =$ ("# span_group_type" pai.html ();
Comment. title = $ ("# thread_title" pai.html ();
Comment. content = content;
$. Ajax ({
Url: '/WebService/GroupService. asmx/AddThreadComment ',
Data: $. toJSON (comment ),
Type: 'post ',
DataType: 'json ',
ContentType: 'application/json; charset = UTF-8 ',
Cache: false,
Success: function (data ){
// Process data. d based on the returned value
},
Error: function (xhr ){
// An exception occurs in the middle. For details, refer to xhr. responseText.
}
});
}

Use $. toJSON (object) directly;
JQuery JSON Plugin: http://code.google.com/p/jquery-json/

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.