angularjs $resource not replacing variable in url template for POST

喜夏-厌秋 提交于 2019-12-08 04:13:24

问题


Using AngularJS 1.2.16 and angular-resource 1.2.16.

I have a resource like:

$resource('api/:variable/path',
    {
        variable:'@variableName'
    });

When I do a get using something like

resourceIns.get({variable:'taco'});

The resulting ajax call replaces :variable properly and i get

api/taco/path

If I do a post like

resourceIns.save({variable:'taco'});

the resulting ajax call looks like

api/path

and 'taco' gets put in the POST body...

I've had trouble finding others complaining about this so, maybe this is what's supposed to happen?

edit: I just discovered that get uses 'variable' and save/POST uses 'variableName' in the above example. Anybody have an explanation for that?

Here's a fiddle showing the situation: fiddle


回答1:


I ran across the same issue or one that presented itself in the same fashion. My resource, too, was not respecting parameters passed in through a .post method.

I was able to get it to work by directly passing in the expected parameters.

Using a $resource:

angular.module('myApp')
  .factory('ModuleProductProducts', function ($resource) {
    return $resource('/module-api/product-products/:siteId/:id/:controller', {
      id: '@id'
    },
    {
        'updateMedia': {
            method: 'POST',
            url: 'module-api/product-products/:id/media/:mediaId',

            // *** Here ***
            params: {
                id: '@id',
                mediaId: '@mediaId'
            }
            // ************
        }
    });
  });


来源:https://stackoverflow.com/questions/24025749/angularjs-resource-not-replacing-variable-in-url-template-for-post

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