【问题标题】:Including placeholder changes ngResource save() method from POST to GET包括占位符将 ngResource save() 方法从 POST 更改为 GET
【发布时间】:2015-06-29 20:24:07
【问题描述】:

一个非常简单的例子。我有一个 RESTful api,我通过以下方式设置我的资源。

app.factory('apiFactory' , ['$resource', 'GLOBALS', 
    function($resource, GLOBALS){
        return {
            Discounts: $resource(GLOBALS.apiPath + 'discounts/:id', {id:'@id'}, {update:{method: 'PUT'}})
        }     
    }
])

然后我像这样在控制器中调用它

var discountResponse = apiFactory.Discounts.save($scope.discount);

在我将 '/:id' 添加到我的 URL 之前,一切正常。我这样做是为了让我的删除方法传递 id。像这样“折扣/6”。

我遇到的问题是,只要我添加占位符,我的 save() 方法就会发送 GET 而不是 POST。

Request URL:http://local:8089/api/discounts
Request Method:GET
Status Code:200 OK

如果我删除占位符,我会得到

Request URL:http://local:8089/api/discounts
Request Method:POST
Status Code:200 OK

一切正常,接受删除请求,现在不映射占位符,因为它不再存在。

我完全不知道为什么。我对 $resource 很陌生,所以我很确定我不理解某些东西。

【问题讨论】:

  • 这里真的很绝望,我开始认为我发现了一个错误。非常感谢任何帮助。

标签: angularjs api rest crud ngresource


【解决方案1】:

答案是针对不同形式的问题提供的,我想我会分享它。

return {
        Discounts: $resource(GLOBALS.apiPath + 'discounts/:id', {id:'@id'} ,{
            save: { 
                method: 'POST', url: GLOBALS.apiPath + "discounts" 
            },
            update: {
                method: 'PUT', url: GLOBALS.apiPath + "discounts/:id" 
            }
        })
    }

看来,为了让 save() 正确发布,我必须在 customConfig 对象中定义一个路径。我不确定为什么这对我来说开箱即用。

这里提供了答案。非常感谢!
ngResource save() strange behaviour

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 2020-07-05
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2014-04-02
    • 2021-01-29
    • 2011-07-11
    相关资源
    最近更新 更多