这是我要序列化的对象:

{"cars": [
   {"make":"volkswagen","model":"golf"},
   {"make":"volkswagen","model":"polo"},
   {"make":"audi","model":"a3"},
   {"make":"bmw","model":"1-series"},
   {"make":"renault","model":"espace"}
]}


之前,我使用jQuery $.param()函数和qs库在node.js上对其进行解码。而且一切正常,我不需要自己解析任何内容。

现在,我决定使用新的Angular 1.4服务$httpParamSerializer摆脱jQuery。解码后,我得到的是objects而不是数组中的strings

 '{"make":"volkswagen","model":"golf"}'


如何在不对数组中的每个对象调用$httpParamSerializer的情况下正确解码使用JSON.parse服务创建的查询字符串?

这是$.param查询字符串:

cars%5B0%5D%5Bmake%5D=volkswagen&cars%5B0%5D%5Bmodel%5D=golf&cars%5B1%5D%5Bmake%5D=volkswagen&cars%5B1%5D%5Bmodel%5D=polo&cars%5B2%5D%5Bmake%5D=audi&cars%5B2%5D%5Bmodel%5D=a3&cars%5B3%5D%5Bmake%5D=bmw&cars%5B3%5D%5Bmodel%5D=1-series&cars%5B4%5D%5Bmake%5D=renault&cars%5B4%5D%5Bmodel%5D=espace&demo%5Bsome%5D=bar


这是$httpParamSerializer查询字符串:

cars=%7B%22make%22:%22volkswagen%22,%22model%22:%22golf%22%7D&cars=%7B%22make%22:%22volkswagen%22,%22model%22:%22polo%22%7D&cars=%7B%22make%22:%22audi%22,%22model%22:%22a3%22%7D&cars=%7B%22make%22:%22bmw%22,%22model%22:%221-series%22%7D&cars=%7B%22make%22:%22renault%22,%22model%22:%22espace%22%7D&demo=%7B%22some%22:%22bar%22%7D`

最佳答案

还有$ HttpParamSerializerJQLikeProvider像jquery param一样进行序列化。从文档中

* Alternative {@link $http `$http`} params serializer that follows
* jQuery's [`param()`](http://api.jquery.com/jquery.param/) method logic.
* The serializer will also sort the params alphabetically.

关于angularjs - 正确解码Angular 1.4 $ httpParamSerializer querystring?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31964143/

10-09 20:28