问题
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