3

When using ngResource/$resource, it is possible to implement custom (de)serialisation by specifying transformRequest/transformResponse. However these only control the request's body (data), so how can one manipulate the query parameters in a GET request?

Specifically, I would like to json-encode all the parameter values.

Simple case:
?user=123 is user with id 123
?user="123" is user with name 123

Complex case:
Passing objects/hashes in a GET request. For example using a mongo-like syntax to specify request criteria/projection. (Please note this question is not about mongo specifcally)

4

1 回答 1

3

您可以为此使用请求拦截器:

$httpProvider.interceptors.push(function() {
  return {
   'request': function(config) {
       //config.params contains query/request parameters
       if (config.params){
         //Do something here...
       }
       return config;
    }
  };
});
于 2014-08-19T08:01:17.633 回答