jQuery ajax传参巧用JSON

      意外发现Jquery调用ajax时候,参数类型为JSON,传参数时候大家一般喜欢拼接字符串,可能有时候会拼接错误,大家可以参考一下底下代码,相信大家在以后使用时候会做出抉择的。其中需要注意的是如果参数涉及中文

。那么一定要设置contentType字符集类型。

 1 function AddComment(content) {
 2     var comment = {};
 3     comment.threadId = $("#div_thread_id").html();
 4     comment.groupId = $("#div_group_id").html();
 5     comment.groupType = $("#div_group_type").html();
 6     comment.title = $("#div_thread_title").html();
 7     comment.content = content;
 8     $.ajax({
 9         url: '/WebService/GroupService.asmx/AddThreadComment',
10         data: $.toJSON(comment),
11         type: 'post',
12         dataType: 'json',
13         contentType: 'application/json;charset=utf-8',
14         cache: false,
15         success: function(data) {
16             //根据返回值data.d处理    
17         },
18         error: function(xhr) {
19             //中间发生异常
20         }
21     });
22 }

 

posted on 2013-04-01 10:36  朝扬  阅读(583)  评论(0编辑  收藏  举报