Search code examples
angularjshttprestful-urlangular-resourcequery-string

Angular resource with integer parameter builds wrong url


I am trying to use the following code to communicate with a RESTFul service:

$scope.makeRequest = function(){
    params = {};
    params['3'] = 'X';
    params['length'] = 5;

    var resource = $resource('/foo', {});
    var result = resource.query(params, function(){
       console.log(result);
    });
}

This request works, and calls the expected url: http://localhost:3000/foo?3=X&length=5

However, when the integer field is the value of the 'length' field minus one:

$scope.makeRequest = function(){
    params = {};
    params['4'] = 'X';
    params['length'] = 5;

    var resource = $resource('/foo', {});
    var result = resource.query(params, function(){
       console.log(result);
    });
}

This snippet ignores the 'length' field, and makes a request to: http://localhost:3000/foo?4=X

My questions are:

  1. Where does this behaviour come from?
  2. Is there a better way to make this request? Can I avoid multiple integer parameters, considering that I would need more than one?

Solution

  • Have you try to just set up an literal object like this:

    params = {'4':'X', 'length': '5'}