您现在的位置是:网站首页> 编程资料编程资料
jQuery的ajax传参巧用JSON使用示例(附Json插件)_AJAX相关_
2023-05-25
421人已围观
简介 jQuery的ajax传参巧用JSON使用示例(附Json插件)_AJAX相关_
jQuery的ajax调用很方便,传参的时候喜欢用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) {
//根据返回值data.d判断是不是成功
},
error: function(xhr) {
//中间发生异常,查看xhr.responseText
}
});
}
这中间最麻烦,最容易出错的也是拼接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
}
});
}
直接用$.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) {
//根据返回值data.d判断是不是成功
},
error: function(xhr) {
//中间发生异常,查看xhr.responseText
}
});
}
这中间最麻烦,最容易出错的也是拼接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
}
});
}
直接用$.toJSON(对象)即可;
jQuery的JSON插件:http://code.google.com/p/jquery-json/
您可能感兴趣的文章:
相关内容
- PHP+AJAX无刷新实现返回天气预报数据_AJAX相关_
- jquery ajax 向后台传递数组参数示例_AJAX相关_
- IIS7中Ajax.AjaxMethod无效的原因及解决方法_AJAX相关_
- ajax读取数据库内容实现二级联动下拉选择菜单示例_AJAX相关_
- AJAX在不同浏览器中XMLHttpRequest对象的生成示例_AJAX相关_
- asp.net中mvc使用ajax提交参数的匹配问题解决探讨_AJAX相关_
- ajax 防止用户反复提交的2种解决方案_AJAX相关_
- 添加后台list给前台select标签赋值简单实现_AJAX相关_
- ajax返回的json内容进行排序使用sort()方法实现_AJAX相关_
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法示例_AJAX相关_
