如何在$.ajax 的data里面传json格式数据
问题一
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"}]}
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "ApplyEdit.aspx/SaveParticipant",
data: 这里该怎么写,
dataType: "json",
complete: function () { },
success: function (result) {
},
error: function (result, status) { }
});
谁能告诉我这个 data:这里该如何写,我要把jsondata传到后台去
问题二
平时用的时候我有时候这么写就可以 data:{"name":name,"sex":sex},
有点时候又必须要这样写才行才能传值成功 data:"{"name":""+name+"","sex":""+sex+""}",
一直不明白为什么,望各位大大帮忙解释下,不胜感激
jquery josn ajax
[解决办法]
将json对象转对对应的json格式的字符串就行了,下载:json2.js导入页面中
<script src="http://www.coding123.net/rardownload/20130106/20130106170832648.js"></script>
<script>
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"}]}
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "ApplyEdit.aspx/SaveParticipant",
data: JSON.stringify(jsondata),//////////////////////
dataType: "json",
complete: function () { },
success: function (result) {
},
error: function (result, status) { }
});
</script>
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"}]} ;
var exdata= jQuery.extend({}, jsondata.Participant[0], jsondata.Participant[1]);
console.log(exdata);
$.ajax({
url: "/tt.jsp",
type: "GET",
data:exdata,
beforeSend: function(){
},
success: function(d){
}
});