AngularJS resource query is misinterpreting object composed by arrays

69 views Asked by At

I have the following resoure:

var get = $resource('http://localhost/', {}, {
        query: {
            method: 'GET',
            headers: { 'Content-Type': 'application/json' },
            params: { 'a': [10, 20, 30] }
        }
    });

My problem is that AngularJS is translating this into a call for this url:

http://localhost/api?a=10&a=20&a=30

On the other end (http://localhost/api), I have a PHP application that is interpreting this just as a = 30.

I think Angular is not interpreting the object param the right way. It should call the url:

http://localhost/api?a%5B0%5D=10&a%5B1%5D=20&a%5B2%5D=30

Which would translate to a[0]=10&a[1]=20&a[2]=30. PHP interprets this as 'a': [10, 20, 30].

How can I make AngularJS translate my query the right way (from the point of view of my PHP application)?

1

There are 1 answers

1
Daan On BEST ANSWER

There is no particular standard for how arrays should be serialized into query strings. Different back ends expect different formats. This current method (in 1.1.x) was chosen specifically because it allows the developer the most flexibility.

You can get your desired serialization by giving setting params to:

params = {
  "a[]": [10, 20, 30]
};