Jquery's Ajax call is very convenient. When passing parameters, you prefer the JSON data format. For example:
JavaScript code, add a comment
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:
JavaScript code that uses json to pass Parameters
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/