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:"' + content + '"}', type: 'post',
dataType: 'json',
contentType: 'application/json;charset=utf-8',
cache: false,
success: function(data) {
//根據(jù)返回值data.d判斷是不是成功
},
error: function(xhr) {
//中間發(fā)生異常,查看xhr.responseText
}
});
}
這中間最麻煩,最容易出錯的也是拼接Json字符串,字符型參數(shù)的值要添加引號,而且對于用戶輸入的文本字段要對',/等進(jìn)行特殊處理意外的機(jī)會,上司給我推薦了一種新的方法,看下面代碼:
代碼如下:
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) {
//根據(jù)返回值data.d處理
},
error: function(xhr) {
//中間發(fā)生異常,具體查看xhr.responseText
}
});
}
直接用$.toJSON(對象)即可;
jQuery的JSON插件:http://code.google.com/p/jquery-json/聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com