0

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
  • 1
    POST would be the correct method for a requests that wants to send such data Commented Jun 10, 2015 at 12:25

1 Answer 1

1

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]
};
Sign up to request clarification or add additional context in comments.

1 Comment

Your suggestion caused AngularJS to query localhost/api?a%5B%5D=10&a%5B%5D=20&a%5B%5D=30 . That's a good workaround.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.