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/