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/