使用方法时,AngularJS $资源调用错误API网址:POST [英] AngularJS $resource calls the wrong API URL when using method:POST

查看:188
本文介绍了使用方法时,AngularJS $资源调用错误API网址:POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是最简单的问题放到一个标题。

总之,我的应用程序是建立在的NodeJS / 前pressjs ,并建立了一个API网址:

编辑:目前的code,我使用的是:

  $ scope.updateProduct = $资源('/ API / UpdateProduct中/:产品/:参数/:值',{},{
  查询:{方法:GET},
  岗位:{方法:POST},
  保存:{方法:把',则params:{品牌:@brand',参数:'@参数',值:'@值'}},
  删除:{方法:'删除'}
});
$ scope.updateProduct.save({
    产品:$ scope.post._id,
    参数:'喜欢',
    价值:$ scope.user._id
  });

在present它调用 / API / UpdateProduct中而不是 / API / UpdateProduct中/<产品> /<参数> / < VALUE> 像它应该/当我执行 $ scope.updateProduct.get()

喜欢它。

在我的控制台我看到(作为一个例子):

  PUT / API / updateBrand /码头%20Eyewear%20Australia / userTags /太阳镜%20classic 200 30毫秒 -  2.31kb

然而,API不实际访问/没有任何反应。有趣的是,如果我去<$c$c>localhost:5000/api/updateBrand/Quay%20Eyewear%20Australia/userTags/sunglasses,%20classic在我的浏览器,它张贴正确的数据和我的数据库更新的产品,所以它肯定与 $资源被调用的方式错误。


解决方案

正如你可以看到 ngResource文档 $资源接收3个参数:

$资源(URL [,paramDefaults] [,行动]);

您正在通过你​​的行动作为参数。<​​/ P>

正确的版本是:

  $ scope.updateProduct = $资源('/ API / UpdateProduct中/:产品/:参数/:值',{},{保存:{方法:POST }});

请注意,它甚至没有必要,因为当你使用 $资源您已经创建默认方式:

  {
    '得到':{方法:GET},
    保存:{方法:POST},
    '查询':{方法:GET,IsArray的:真正},
    删除:{方法:删除},
    删除:{方法:'删除'}
};

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.

解决方案

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

这篇关于使用方法时,AngularJS $资源调用错误API网址:POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆