jquery Ajax to use JSON using the example (with the JSON plugin) _ajax related

Source: Internet
Author: User
Tags tojson
The Ajax invocation of jquery is convenient, and the data format of JSON is preferred when passing in the parameter. Like what:
Copy Code code as follows:

function AddComment (content) {
var threadId = $ ("#span_thread_id"). html ();
var groupId = $ ("#span_group_id"). html ();
var grouptype = $ ("#span_group_type"). html ();
var title = $ ("#thread_title"). html ();
var content = Content.replace (/\x22/g, ' "");
$.ajax ({
URL: '/webservice/groupservice.asmx/addthreadcomment ',
Data: ' {threadId: ' + threadId + ', groupId: ' + groupId + ', GroupType: ' + grouptype + ', title: "' + title + '", Content: "' + C Ontent + ' "} ', type: ' Post ',
DataType: ' JSON ',
ContentType: ' Application/json;charset=utf-8 ',
Cache:false,
Success:function (data) {
Based on the return value DATA.D is judged to be successful
},
Error:function (XHR) {
An exception occurred in the middle, view Xhr.responsetext
}
});
}

This is the most troublesome, the most error is also the concatenation of JSON strings, character parameter values to add quotes, and for user input text fields to ',/and so special handling

Unexpected opportunity, the boss gave me a new way to look at the following code:
Copy Code code as follows:

function AddComment (content) {
var comment = {};
Comment.threadid = $ ("#span_thread_id"). html ();
Comment.groupid = $ ("#span_group_id"). html ();
Comment.grouptype = $ ("#span_group_type"). html ();
Comment.title = $ ("#thread_title"). 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) {
DATA.D processing based on return value
},
Error:function (XHR) {
An exception occurred in the middle, specific view Xhr.responsetext
}
});
}

Directly with $.tojson (object) can be;
jquery's 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.