jQuery的ajax传参巧用JSON (转Json插件)
来源:互联网 发布:linux开发视频教程 编辑:程序博客网 时间:2024/06/05 01:37
jQuery的ajax传参巧用JSON (转Json插件)
jQuery的ajax调用很方便,传参的时候喜欢用Json的数据格式。比如:
JavaScript代码,增加一个评论
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) {
//根据返回值data.d判断是不是成功
},
error: function(xhr) {
//中间发生异常,查看xhr.responseText
}
});
}
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) {
//根据返回值data.d判断是不是成功
},
error: function(xhr) {
//中间发生异常,查看xhr.responseText
}
});
}
这中间最麻烦,最容易出错的也是拼接Json字符串,字符型参数的值要添加引号,而且对于用户输入的文本字段要对',/等进行特殊处理
意外的机会,上司给我推荐了一种新的方法,看下面代码:
JavaScript代码,巧用JSON传参数
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) {
//根据返回值data.d处理
},
error: function(xhr) {
//中间发生异常,具体查看xhr.responseText
}
});
}
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) {
//根据返回值data.d处理
},
error: function(xhr) {
//中间发生异常,具体查看xhr.responseText
}
});
}
直接用$.toJSON(对象)即可;
jQuery的JSON插件:http://code.google.com/p/jquery-json/
- jQuery的ajax传参巧用JSON (转Json插件)
- jQuery的ajax传参巧用JSON
- jquery的json插件
- jquery插件-table转Json数据插件
- Jquery Step00-$.Ajax Json
- Jquery Step01-$.Ajax Json
- jquery ajax json parsererror
- jquery php ajax json
- AJax+Json+JQuery----前沿
- JSON AJAX JQuery
- ajax+jQuery+json
- AJAX+jQuery+JSON
- jquery+Ajax +Json绑定
- php+jquery +ajax +json
- jquery ajax json parsererror
- jquery ajax struts2 json
- struts2+jquery+ajax+json
- jquery------------ajax+json解析
- Struts2 s:if 界面判断集合or对象
- RGB图像缩放
- sql 留心空洞
- QList和QVector等容器的区别
- Phone Gap WebApp制作之整体流程规划.
- jQuery的ajax传参巧用JSON (转Json插件)
- 数据传输 -- 字符串报文
- 查看so文件中的源文件路径
- xml Schema
- MAC OS命令行使用详解
- 图解GIT
- 赋值的另一种写法
- 编译原理
- 2013艾瑞年度峰会-易车-如何塑造新营销生态链