jQuery的ajax傳參巧用JSON使用示例(附Json插件)
更新時間:2013年08月07日 16:54:56 作者:
jQuery的ajax調(diào)用很方便,傳參的時候喜歡用Json的數(shù)據(jù)格式,使用示例代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助
jQuery的ajax調(diào)用很方便,傳參的時候喜歡用Json的數(shù)據(jù)格式。比如:
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ù)的值要添加引號,而且對于用戶輸入的文本字段要對',/等進行特殊處理
意外的機會,上司給我推薦了一種新的方法,看下面代碼:
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/
復制代碼 代碼如下:
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ù)的值要添加引號,而且對于用戶輸入的文本字段要對',/等進行特殊處理
意外的機會,上司給我推薦了一種新的方法,看下面代碼:
復制代碼 代碼如下:
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/
您可能感興趣的文章:
相關(guān)文章
使用Jquery+Ajax+Json如何實現(xiàn)分頁顯示附JAVA+JQuery實現(xiàn)異步分頁
本文給大家介紹基于jquery+ajax+json實現(xiàn)數(shù)據(jù)分頁顯示,以及JAVA+JQuery實現(xiàn)異步分頁,本文代碼簡單易懂,非常具有參考價值,感興趣的朋友一起學習吧2015-10-10深入淺析AjaxFileUpload實現(xiàn)單個文件的 Ajax 文件上傳庫
jQuery.AjaxFileUpload.js是一款jQuery插件,用于通過ajax上傳文件。本文給大家介紹AjaxFileUpload實現(xiàn)單個文件的 Ajax 文件上傳庫,對此感興趣的朋友一起學習吧2016-04-04通過抓取淘寶評論為例講解Python爬取ajax動態(tài)生成的數(shù)據(jù)(經(jīng)典)
在學習python的時候,一定會遇到網(wǎng)站內(nèi)容是通過 ajax動態(tài)請求、異步刷新生成的json數(shù)據(jù) 的情況,并且通過python使用之前爬取靜態(tài)網(wǎng)頁內(nèi)容的方式是不可以實現(xiàn)的,所以這篇文章將要講述如果在python中爬取ajax動態(tài)生成的數(shù)據(jù)。2015-10-10AJAX中同時發(fā)送多個請求XMLHttpRequest對象處理方法
AJAX中同時發(fā)送多個請求XMLHttpRequest對象處理方法...2007-04-04JQuery ajax返回JSON時的處理方式 (三種方式)
json數(shù)據(jù)是一種經(jīng)型的實時數(shù)據(jù)交互的數(shù)據(jù)存儲方法,使用到最多的應該是ajax與json配合使用了,下面由腳本之家小編給大家分享JQuery ajax返回JSON時的處理方式 (三種方式),需要的朋友可以參考下2015-09-09