ngResource附加POST参数网址参数、网址、ngResource、POST

2023-09-13 03:15:28 作者:四海八荒第一小短腿

我有一个角度的服务,看起来是这样的。在这里,我想提出一个POST请求。

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'
                        }
                    }
                }
            );
        })

问题是,上述服务使POST请求和 PARAMS 数据,表单数据发送,还追加 PARAMS 数据URL作为查询字符串。我能避免吗?

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?

推荐答案

我觉得你发生冲突如何使用 $资源。您应该创建$资源实例,如果他们的模型,并设置您要发布的对象的属性。

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: ...
});

最后,$资源具有内置的 $保存()使用POST,你可以改用创建自定义的 $创建( )操作。

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