ngResource appends POST parameters to url

爱⌒轻易说出口 提交于 2020-01-13 12:12:29

问题


I have an angular service that looks like this. Here i am making a POST request.

.factory("Apples", function ($resource, HOST) {

            return $resource(
                HOST + "/apples",
                {},
                {
                    create: {
                        method: 'POST',
                        params: {
                            tree_id: '@treeId',
                            name: '@name',
                            color: '@color'
                        }
                    }
                }
            );
        })

The problem is that the above service makes a POST request and sends the params data as form data but also appends the params data to the url as query string. Can i avoid that?


回答1:


I think you're conflicted on how to use $resource. You should create $resource instances as if they were models and set the attributes on the object that you want to POST.

With your Apples $resource defined as above:

var apple = new Apples();

apple.color = ...
apple.name = ...
apple.tree_id = ...

apple.$create()

Or you could just use the $resource class directly:

Apples.create({
    apple.color: ...
    apple.name: ...
    apple.tree_id: ...
});

Finally, $resource has the built-in $save() that uses POST which you can use instead of creating a custom $create() action.



来源:https://stackoverflow.com/questions/20458140/ngresource-appends-post-parameters-to-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!