var paras = {}; paras.name='jack'; $.ajax({ type:'get', url:'/testchart', data:$.toJSON(paras), dataType:'json', contentType: 'application/json;charset=utf-8', success: onDataReceived });
这样传到后台,直接获取paras =request.GET就能将整个paras获取到,然后使用paras['name']就能得到传过来的name。
var paras = {}; paras.rows=[]; var rowstr={}; rowstr.property =null; rowstr.filter =null; rowstr.casted_type =null; paras.rows.push(rowstr); paras.bool_op ='or'; paras.arb_event ='Add Friend Receive'; paras.action='by'; paras.unit='day'; paras.type='general'; paras.limit = 40; paras.chart_type= 'line'; paras.chart_keys=['Add Friend Request']; paras.from_date = '2012-09-10'; paras.to_date = '2012-10-10'; paras.project ='pirates'; $.ajax({ type:'get', url:'/testchart', data:paras, dataType:'json', contentType: 'application/json;charset=utf-8', success: onDataReceived });
情况有点儿特殊,在paras里传入一个是数组的对象rows, 使用这样的方法在后台打印不出结果。也就是paras['rows']根本不响应。直接打印request.GET,发现结果很诡异:
<QueryDict: {u
'project'
: [u
'pirates'
], u
'rows[0][filter]'
: [u
''
], u
'chart_keys[]'
: [u
'Add Friend Request'
], u
'rows[0][property]'
: [u
''
], u
'rows[0][casted_type]'
: [u
''
], u
'bool_op'
: [u
'or'
], u
'from_date'
: [u
'2012-09-10'
], u
'limit'
: [u
'40'
], u
'chart_type'
: [u
'line'
], u
'action'
: [u
'by'
], u
'type'
: [u
'general'
], u
'arb_event'
: [u
'Add Friend Receive'
], u
'unit'
: [u
'day'
], u
'to_date'
: [u
'2012-10-10'
]}>
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 } });}
$.ajax({
type:
'get'
,
url:
'/testchart'
,
data:$.toJSON(paras),
dataType:
'json'
,
contentType:
'application/json;charset=utf-8'
,
success: onDataReceived
});
然后打印出来的结果为:
<QueryDict: {u '{"rows":[{"property":null,"filter":null,"casted_type":null}],"bool_op":"or","arb_event":"Add Friend Receive","action":"by","unit":"day","type":"general","limit":40,"chart_type":"line","chart_keys":["Add Friend Request"],"from_date":"2012-09-10","to_date":"2012-10-10","project":"pirates"}' : [u '' ]}> |