2

I've recently upgraded to AngularJS 1.2.0, and there a problem with passing array that breaks the site. In an example $http request I am passing fields like so:

$http.get({
                url : 'users',
                data : {
                    fields : 'user_id, user_name',
                    conditions : {
                        customer_id : current_site_id
                    },
                    join : ['customer_users']
                }
            });

In the last stable release of 1.0.8, the [] for the join was preserved, so in the php side it showed up as array and was iterable. In 1.2.0, the array is removed and the parameters pass as such by the browser to the server:

conditions:{"customer_id":"9a83a3db-673e-407f-9a0d-1f804c4dcd01"}
fields:user_id, user_name
join:customer_users //<---- THIS SHOULD BE AN ARRAY!

Because its not an iterable object, it breaks the php side. So aside from renaming all array variables from ['xyz'] to {'0': 'xyz'}, to mimic the array behavior though not optimal, how can I ensure angularJS passes the value as an array?

1 Answer 1

1

I think your example should not work at all. It should be this:

$http({
    method: 'GET',
    url : 'users',
    params : {
        fields : 'user_id, user_name',
        conditions : {
            customer_id : current_site_id
        },
        join : ['customer_users']
    }
});

And angular does the right job parsing the url since 1.1.1. The spec says if you have an array it'll be parsed like this ?join=item1&join=item2. If the array only have one item it'll be ?join=item1.

On the other hand jQuery.param() will parse it like this: ?join[]=item1&join[]=item2 And it - if I'm right - will ensure that $_GET['join'] always be an array.

All in all, try 'join[]' : ['customer_users']. It might work.

Sign up to request clarification or add additional context in comments.

3 Comments

A possible solution but that solution also requires me to go through each $http I making and modifying it. Doable yes but not optimal.
Well, you can always intercept the request. (docs.angularjs.org/api/ng.$http#description_interceptors) In a request interceptor, you'll have a config.params. Feel free to transform as you wish.
Thanks! Thats a much better solution I can write

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.