简体   繁体   English

如何从RESTANGULAR HTTP请求中删除这些自定义查询参数?

[英]How do I remove these custom query parameters from my RESTANGULAR HTTP Request?

I'm trying to remove custom parameters from the following HTTP request on my REST API: 我正在尝试从REST API上的以下HTTP请求中删除自定义参数:

I want to turn http://localhost:3000/users?_page=1&_perPage=30&_sortDir=DESC&_sortField=id 我想转为http:// localhost:3000 / users?_page = 1&_perPage = 30&_sortDir = DESC&_sortField = id

into http://localhost:3000/users 进入http:// localhost:3000 / users

I am using ng-admin, which is an AngularJS admin panel, they provide a page on changing query parameters here: https://github.com/marmelab/ng-admin/blob/master/doc/API-mapping.md 我正在使用ng-admin(这是AngularJS管理面板),它们在此处提供有关更改查询参数的页面: https : //github.com/marmelab/ng-admin/blob/master/doc/API-mapping.md

I have used some of their code and tried to use the following code to implement what I'm trying to do but it won't work. 我已经使用了他们的一些代码,并尝试使用以下代码来实现我正在尝试执行的操作,但是它将无法正常工作。

myApp.config(['RestangularProvider', function(RestangularProvider) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
    if (operation == 'getList' && what == 'entityName') {
        delete params._page;
        delete params._perPage;
        delete params._sortField; 
        delete params._sortDir; 
    }
    console.log({ params: params });
});
}]);

Finally, how can I check the actual HTTP request that is sent through once the interceptors have been utilised, chrome developer tools seems to only display the original request with all parameters even though I've implemented the above methods. 最后,一旦使用拦截器,如何检查发送的实际HTTP请求,即使我已经实现了上述方法,Chrome开发者工具似乎也只能显示带有所有参数的原始请求。 I think this is because the interceptor works after the browser implements the request. 我认为这是因为拦截器在浏览器实现请求后起作用。

You should return whole request from interceptor otherwise there won't be any changes on request. 您应该从拦截器返回整个请求,否则请求中不会有任何更改。 It is written at documents. 它写在文件上。

addFullRequestInterceptor addFullRequestInterceptor

addFullRequestInterceptor addFullRequestInterceptor

This adds a new fullRequestInterceptor. 这将添加一个新的fullRequestInterceptor。 The fullRequestInterceptor is similar to the requestInterceptor but more powerful. fullRequestInterceptor与requestInterceptor类似,但功能更强大。 It lets you change the element, the request parameters and the headers as well. 它允许您更改元素,请求参数和标头。

It's a function that receives the same as the requestInterceptor plus the headers and the query parameters (in that order). 该函数接收与req​​uestInterceptor相同的函数以及标头和查询参数(按此顺序)。

It can return an object with any (or all) of following properties: 它可以返回具有以下任何(或全部)属性的对象:

headers: The headers to send params: The request parameters to send element: The element to send httpConfig: The httpConfig to call with If a property isn't returned, the one sent is used. headers:发送参数的headers:发送参数的请求元素:发送httpConfig的元素:要与httpConfig一起调用的元素如果未返回属性,则使用发送的那个。

at the last lines it said If a property isn't returned, the one sent is used. 在最后一行说如果没有返回属性,则使用发送的属性。 so if you send changed properties in an object then you should be fine... 因此,如果您将更改后的属性发送到对象中,则应该可以...

myApp.config(['RestangularProvider', function(RestangularProvider) {
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
    if (operation == 'getList' && what == 'entityName') {
        delete params._page;
        delete params._perPage;
        delete params._sortField; 
        delete params._sortDir; 
    }
    console.log({ params: params });

    // return changed properties which is params in this case
    return { params: params };


});
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM