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ù)的值要添加引號,而且對于用戶輸入的文本字段要對',/等進(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/
復(fù)制代碼 代碼如下:
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ī)會,上司給我推薦了一種新的方法,看下面代碼:
復(fù)制代碼 代碼如下:
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/
您可能感興趣的文章:
- html+js+php一次原始的Ajax請求示例
- jquery教程ajax請求json數(shù)據(jù)示例
- 使用$.getJSON實現(xiàn)跨域ajax請求示例代碼
- jQuery中使用Ajax獲取JSON格式數(shù)據(jù)示例代碼
- jquery ajax對特殊字符進(jìn)行轉(zhuǎn)義防止js注入使用示例
- 通過AJAX的JS、JQuery兩種方式解析XML示例介紹
- ajax后臺處理返回json值示例代碼
- AJAX如何接收J(rèn)SON數(shù)據(jù)示例介紹
- 淺析ajax請求json數(shù)據(jù)并用js解析(示例分析)
- JavaScript中的ajax功能的概念和示例詳解
相關(guān)文章
甩掉ashx和asmx使用jQuery.ajaxWebService請求WebMethod簡練處理Ajax
這篇文章主要介紹了甩掉ashx和asmx使用jQuery.ajaxWebService請求WebMethod簡練處理Ajax的相關(guān)資料,需要的朋友可以參考下2016-08-08
ajax動態(tài)查詢數(shù)據(jù)庫數(shù)據(jù)并顯示在前臺的方法
今天小編就為大家分享一篇ajax動態(tài)查詢數(shù)據(jù)庫數(shù)據(jù)并顯示在前臺的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
ajax請求post和get的區(qū)別以及get post的選擇
這篇文章主要介紹了ajax請求post和get的區(qū)別以及get post的選擇,需要的朋友可以參考下2014-06-06
用javascript實現(xiàn)頁面無刷新更新數(shù)據(jù)
用javascript實現(xiàn)頁面無刷新更新數(shù)據(jù)...2006-06-06
bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼
這篇文章主要介紹了bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
用AJAX技術(shù)實現(xiàn)在自己Blog上聚合并顯示朋友Blog的最新文章
在自己Blog上聚合并顯示朋友Blog的最新文章,這樣方便自己及時了解朋友的消息,另外,也方便訪問者找到和本Blog相關(guān)的blog和文章2014-05-05
Ajax獲取回調(diào)函數(shù)無法賦值給全局變量的問題
這篇文章主要介紹了Ajax獲取回調(diào)函數(shù)無法賦值給全局變量的問題,需要的朋友可以參考下2018-06-06

