AngularJS $resource calls the wrong API URL when using method:POST

China☆狼群 提交于 2019-12-10 16:48:14

问题


Not the easiest issue to put into a title.

Anyhow, my app is built on nodejs/expressjsand has an API set up for the url:

EDIT: The current code I'm using is:

$scope.updateProduct = $resource('/api/updateProduct/:product/:param/:value',{},{
  query: {method:'GET'},
  post: {method:'POST'},
  save: {method:'PUT', params: {brand: '@brand', param:'@param', value:'@value'}},
  remove: {method:'DELETE'}
});
$scope.updateProduct.save({
    product : $scope.post._id, 
    param: 'likes', 
    value: $scope.user._id
  }); 

At present it calls /api/updateProduct instead of /api/updateProduct/<product>/<param>/<value> like it's supposed to / like it does when I perform $scope.updateProduct.get().

In my console I see (as an example):

 PUT /api/updateBrand/Quay%20Eyewear%20Australia/userTags/sunglasses,%20classic 200 30ms - 2.31kb

However, the API isn't actually accessed/nothing happens. Interestingly, if I go to localhost:5000/api/updateBrand/Quay%20Eyewear%20Australia/userTags/sunglasses,%20classic in my browser, it posts the correct data and updates the product in my database, so it's definitely an error with the way the $resource is being called.


回答1:


As you can see in ngResource docs, $resource receive 3 parameters:

$resource(url[, paramDefaults][, actions]);

You are passing your action as a parameter.

The correct version would be:

$scope.updateProduct = $resource('/api/updateProduct/:product/:param/:value',{}, {'save':{method:'POST'}});

Note that it isn't even necessary, because when you use $resource you already create the default methods:

{ 
    'get':    {method:'GET'},
    'save':   {method:'POST'},
    'query':  {method:'GET', isArray:true},
    'remove': {method:'DELETE'},
    'delete': {method:'DELETE'} 
};



回答2:


First of all, your have defined the save() to have a parameter called "brand", but in your url template and in the call to save(), you are using "product". I guess it's a typo.

You say when you are using browser to visit the url, it works well; but when angular is make a PUT request to the same url, nothing is happening. This indicates that your backend is configure to process only GET requests for this particular url pattern. Therefore, you need to make sure that your backend is accepting PUT requests.




回答3:


I was struggling with this issue and was able to pass parameters to the resource by doing the equivalent call below (notice the '$' before 'save').

$scope.updateProduct.$save({
    product : $scope.post._id, 
    param: 'likes', 
    value: $scope.user._id
});  

I also did not define a 'save' method in the resource. According to Angular docs:

"Calling these methods (meaning non-GET methods) invoke $http on the url template with the given method, params and headers. When the data is returned from the server then the object is an instance of the resource type and all of the non-GET methods are available with $ prefix. This allows you to easily support CRUD operations (create, read, update, delete) on server-side data."



来源:https://stackoverflow.com/questions/19540139/angularjs-resource-calls-the-wrong-api-url-when-using-methodpost

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