AngularJS: sending requests via $ resource with array type parameters (flags, multi selectors, etc.)

I'm having trouble trying to use the $ resource library in AngularJS to send a correctly serialized GET request when there is an array of checkboxes (client_status) in my GET parameters.

This is the code that I have right now in the controller:

$scope.filters = { client_status: ["CLIENT_STATUS_FORMER", "CLIENT_STATUS_ACTIVE"], client_reference: "e" } $scope.records = Client.get($scope.filters, function(data){ ... } 

The above will send the following GET request:

 f.json?client_reference=e&client_status=CLIENT_STATUS_FORMER,CLIENT_STATUS_ACTIVE 

However, from what I understand, the above seems like this is not the correct format. Can someone get me a little bit involved? I expect the following:

 f.json?client_reference=e&client_status%5B%5D=CLIENT_STATUS_ACTIVE&client_status%5B%5D=CLIENT_STATUS_FORMER 

Your help is greatly appreciated.

Thomas

+4
source share
2 answers

You can accomplish this with $ resource by changing client_status to client_status [] as follows:

 $scope.filters = { 'client_status[]': ["CLIENT_STATUS_FORMER", "CLIENT_STATUS_ACTIVE"], client_reference: "e" } 

I created a plunker for this: http://plnkr.co/edit/QXIaY8XlMUfOy7RNMKKv?p=info

I put a dummy URL there, so if you use Firebug or Chrome dev tools when plunker starts, you will see the http://example.com/api request showing the correct addition of [] to the GET request.

A similar solution was noted in the answer to this question: fooobar.com/questions/606675 / ...

+3
source

Here's how I did it with the $ http provider:

 $http({ url:'api/api.asp', method: 'GET', params: { client_status: ["CLIENT_STATUS_FORMER", "CLIENT_STATUS_ACTIVE"], client_reference: "e" } }).then(function (result) { $scope.test = result.data; }); 

The server call becomes:

 api/api.asp?client_reference=e&client_status=%5B%22CLIENT_STATUS_FORMER%22%2C%22CLIENT_STATUS_ACTIVE%22%5D 

And on the server (here is the classic asp vbscript):

 <% Response.Write Request.QueryString("client_status") %> 

What is displayed:

 ["CLIENT_STATUS_FORMER","CLIENT_STATUS_ACTIVE"] 

And you can use it as a regular array.

EDIT: It should be very similar to the $ resource provider:

 $resource('api/api.asp', {}, { get: { method: 'GET', params: { client_status: ["CLIENT_STATUS_FORMER", "CLIENT_STATUS_ACTIVE"], client_reference: "e" } } ); 
0
source

All Articles