angularjs - $http.get doesn't work with arrays with only one element. How do I specify the param type? -
i'm using angular's $http.get() method query api:
var arr = ['foo','bar']; return $http.get("api/foo", { params: { sampleids: arr} }); this results in request /api/foo?sampleids=115&sampleids=116 works ok. if reduce size of array single element, however, becomes /api/foo?sampleids=115, express (node.js) fails interpret array.
if angular sent query /api/foo?sampleids[]=115 instead, should work fine. there way can tell that?
you can use paramserializer property in request config customize how parameters object gets converted string.
return $http.get("api/foo", { params: { myarray: arr}, paramserializer: function (params) { // return string... } }); you can @ default implementation here, if you're not sure how go serializing object: https://github.com/angular/angular.js/blob/master/src/ng/http.js#l27.
additionally, if provide string rather function, service name - helpful if want reuse it. there's $httpparamserializerjqlike service provided default, serializes them in same format jquery's param() function.
Comments
Post a Comment