Angular 资源自定义 URL 使用查询字符串和 POST 参数 [英] Angular resource custom URL is using query strings and POST parameters
问题描述
我在我的应用程序中为 Angular 资源编写了一个自定义方法来激活用户.API 端点是 /users/activate
并且必须将激活码发送到该端点.这是我的资源的样子:
I've written a custom method on an Angular resource in my application for activating a user. The API endpoint is /users/activate
and an activation code must be PUTed to this endpoint. This is what my resource looks like:
app.factory('User', ['$resource',
function($resource){
return $resource('http://api.site.dev/users/:id', {id: '@id'}, {
activate: {method:'PUT', params:{code: '@code'}, url: 'http://api.site.dev/users/activate'}
});
}]);
我在我的控制器中像这样使用它:
and I'm using it in my controller like so:
User.activate({code: $routeParams.code});
从 Chrome 的网络日志中可以看出,激活码是在查询字符串和请求正文中发送的:
As you can see from the network log on Chrome, the activation code is being sent in the query string and request body:
如何将资源更改为只在请求正文中传递激活码而不是在查询字符串中传递?
How can I change the resource to just pass the activation code in the request body and not in the query string?
推荐答案
只需从动作声明中删除 params
:
Just remove params
from the action declaration:
activate: {method:'PUT', url: 'http://api.site.dev/users/activate'}
这篇关于Angular 资源自定义 URL 使用查询字符串和 POST 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!