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

