0

My resource is:

angular.module('myApp.services')
    .factory('MyResource', ['$resource', function ($resource) {
        return $resource('http://example.org', {}, {});
    }]);

How I do a GET request is:

MyResource.get({z: 5, a: 4});

The URL generated by AngularJS is:

http://example.org/?a=4&z=5

How I want the URL to be is:

http://example.org/?z=5&a=4

Any solutions?

P.S. I thought using an interceptor could do the trick but there is no method for intercepting a request, but there are optional methods called response and responseError. This is because interceptors for $resource and $http are different. See: $resource and $http

hswner
  • 582
  • 1
  • 4
  • 17
  • 2
    You can't guarantee the order of your object's properties [by design](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). – Maen Feb 04 '16 at 12:05
  • 1
    but why you need this? order in parameters not matter – Grundy Feb 04 '16 at 12:06
  • @Bigood thanks for your comment. That may be the reason why AngularJS sorts the parameters alphabetically. – hswner Feb 04 '16 at 12:31
  • @Grundy In fact, the order of parameters doesn't matter for me. But the API I rely on forces this behavior for some endpoints. – hswner Feb 04 '16 at 12:34

1 Answers1

1

You can try this. I simply prefer this for any Service call

var app=angular.module("Test",[]);

app.service('somename', function ($http) {

this.GetData = function (z,a) {
    return $http.get('http://example.org/?z='+z+'&a='+a);
}
});

app.controller("demo",function($scope,somename){

var d=somename.GetData(4,5);

});
Parshwa Shah
  • 331
  • 4
  • 14
  • 1
    Thanks. Good workaround, but not the best solution :( Because it uses $http instead of $resource and you build up the url manually, which is hard in most cases. $resource is a good abstraction of $http for RESTful services and gives many advantages. – hswner Feb 04 '16 at 12:38
  • But, in case I feel running out of time, that's the exact approach I would try ;) – hswner Feb 04 '16 at 12:40