[关闭]
@52fhy 2016-03-05T14:42:55.000000Z 字数 602 阅读 422

Zepto与jQuery笔记

JavaScript


$.ajax

在zepto里:

  1. var data = {'score': ["1", "2", "3"], 'content': "test"};
  2. data = JSON.stringify(data);
  3. $.ajax({
  4. url: 'api/comment/create',
  5. data: data,
  6. dataType: 'json', //返回的数据格式
  7. type: 'POST',
  8. success: function(response){},
  9. error: function(response){}
  10. });

1) 如果data未使用JSON.stringify()处理,默认传递数据是:

  1. 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()处理后传递的数据是:

  1. {"score":["1","2","3"],"content":"test"}

第二种方式,我们在服务端(以下以PHP为例)直接使用

  1. json_decode(file_get_contents('php://input'), true);

便可处理成数组。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注