@52fhy
2016-03-05T14:42:55.000000Z
字数 602
阅读 422
JavaScript
$.ajax
在zepto里:
var data = {'score': ["1", "2", "3"], 'content': "test"};
data = JSON.stringify(data);
$.ajax({
url: 'api/comment/create',
data: data,
dataType: 'json', //返回的数据格式
type: 'POST',
success: function(response){},
error: function(response){}
});
1) 如果data
未使用JSON.stringify()
处理,默认传递数据是:
score%5B%5D=1&score%5B%5D=2&score%5B%5D=3&content=test
原因:如果是GET请求,它会自动被作为参数拼接到url上。非String对象将通过 $.param
得到序列化字符串。参考:http://www.css88.com/doc/zeptojs_api/#$.ajax
2) 使用JSON.stringify()
处理后传递的数据是:
{"score":["1","2","3"],"content":"test"}
第二种方式,我们在服务端(以下以PHP为例)直接使用
json_decode(file_get_contents('php://input'), true);
便可处理成数组。