ajax中JSON.stringify()和JSON.parse()方法的使用

我们平时使用ajax向后台传递数据时,通常会传递json格式的数据,当然这里还有其它格式,比如xml、html、script、text、jsonp格式。

json类型的数据包含json对象和json类型的字符串

JSON.stringify(),将JSON对象转换为JSON类型的字符串

JSON.parse(),将JSON类型的字符串转换为JSON对象

使用详情见下面4个示例。


1、直接传递json对象,示例如下:
[html]  view plain  copy
  1. <span style="font-size:14px;"><script>  
  2. var jsondata={"Participant":[{"Name_1":"1","Position_1":"1","Tel_1":"1","Mobile_1":"1","Ohter_1":"1"},{"Name_2":"1","Position_2":"1","Tel_2":"2","Mobile_2":"2","Ohter_2":"2"}]}  
  3. $.ajax({  
  4.                 type: "POST",  
  5.                 contentType: "application/json;charset=utf-8",  
  6.                 url: "ApplyEdit.aspx/SaveParticipant",  
  7.                 data: jsondata,  
  8.                 dataType: "json",  
  9.                 complete: function () { },  
  10.                 success: function (result) {  
  11.                 },  
  12.                 error: function (result, status) { }  
  13.             });  
  14. </script></span>  



2、使用JSON.stringify(),将JSON对象转换为JSON类型的字符串示例如下:
[html]  view plain  copy
  1. <span style="font-size:14px;"><script>  
  2. var jsondata={"Participant":[{"Name_1":"1","Position_1":"1","Tel_1":"1","Mobile_1":"1","Ohter_1":"1"},{"Name_2":"1","Position_2":"1","Tel_2":"2","Mobile_2":"2","Ohter_2":"2"}]}  
  3. $.ajax({  
  4.                 type: "POST",  
  5.                 contentType: "application/json;charset=utf-8",  
  6.                 url: "ApplyEdit.aspx/SaveParticipant",  
  7.                 data: JSON.stringify(jsondata),  
  8.                 dataType: "json",  
  9.                 complete: function () { },  
  10.                 success: function (result) {  
  11.                 },  
  12.                 error: function (result, status) { }  
  13.             });  
  14. </script></span>  


3、直接传递JSON类型的字符串,如下:

[html]  view plain  copy
  1. <span style="font-size:14px;"><script>  
  2. var jsondata="{\"name\":\""+name+"\",\"password\":\""+password+"\"}";  
  3. $.ajax({  
  4.                 type: "POST",  
  5.                 contentType: "application/json;charset=utf-8",  
  6.                 url: "ApplyEdit.aspx/SaveParticipant",  
  7.                 data: jsondata,  
  8.                 dataType: "json",  
  9.                 complete: function () { },  
  10.                 success: function (result) {  
  11.                 },  
  12.                 error: function (result, status) { }  
  13.             });  
  14. </script></span>  



4、使用JSON.parse(),将JSON类型的字符串转换为JSON对象,示例如下:

[html]  view plain  copy
  1. <span style="font-size:14px;"><script>  
  2. var jsondata="{\"name\":\""+name+"\",\"password\":\""+password+"\"}";  
  3. $.ajax({  
  4.                 type: "POST",  
  5.                 contentType: "application/json;charset=utf-8",  
  6.                 url: "ApplyEdit.aspx/SaveParticipant",  
  7.                 data: JSON.parse(jsondata),  
  8.                 dataType: "json",  
  9.                 complete: function () { },  
  10.                 success: function (result) {  
  11.                 },  
  12.                 error: function (result, status) { }  
  13.             });  
  14. </script></span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值