AngularJS - Get和Post的$ resource不同的URL

Mom*_*ntH 23 angularjs

$ resource非常棒,提供了处理Web服务的非常方便的方法.如果必须在不同的URL上执行GET和POST,该怎么办?

例如,GET URL是,http://localhost/pleaseGethere/:id 而POST URL http://localhost/pleasePosthere没有任何参数

小智 55

使用[actions]的'url'属性覆盖默认网址.

$resource(url, [paramDefaults], [actions], options);
Run Code Online (Sandbox Code Playgroud)

例如:

$resource('http://localhost/pleaseGethere/:id',{},{
    getMethod:{
        method:'GET',
        isArray:true
    }
    postMethod:{
        url:'http://localhost/pleasePosthere',
        method:'POST',
        isArray:false
    }
}
Run Code Online (Sandbox Code Playgroud)

Angular $资源的用法:http://docs.angularjs.org/api/ngResource/service/$resource


小智 32

您应该能够将URL公开为参数.我能够做到这一点:

$provide.factory('twitterResource', [
    '$resource',
    function($resource) {
        return $resource(
            'https://:url/:action',
            {
                url: 'search.twitter.com',
                action: 'search.json',
                q: '#ThingsYouSayToYourBestFriend',
                callback: 'JSON_CALLBACK'
            },
            {
                get: {
                    method: 'JSONP'
                }
            }
        );
    }
]);
Run Code Online (Sandbox Code Playgroud)

然后,您可以覆盖GET通话中的URL .

我在真正的简短测试中发现的一个警告是,如果我包含http://在URL字符串中,它就不起作用了.我没有收到错误消息.它什么也没做.

  • 问题是url参数被编码,这就是'http://'或带有'/'的任何东西都会失败的原因.有任何想法吗? (2认同)
  • 对于提问者正在寻找的内容,这个答案有点过于复杂--Iris的回答很明确. (2认同)

Pet*_*ela 9

如果您将带有param名称的哈希添加到$ resource调用中:

$resource('localhost/pleaseGethere/:id', {id: '@id'});
Run Code Online (Sandbox Code Playgroud)

然后在调用函数时将:id映射到id param(这将调用GET localhost/pleaseGethere/123):

Resource.get({id: 123});
Run Code Online (Sandbox Code Playgroud)

对于POST,您只是不分配id参数:

Resource.post({}, {name: "Joe"});
Run Code Online (Sandbox Code Playgroud)

将调用正确的URL,在本例中为POST localhost/pleaseGethere(尾部斜杠由ngResource剥离).

有关详细信息,请参阅http://docs.angularjs.org/api/ngResource.$resource - >示例 - >信用卡资源.


jmu*_*sch 5

除了Iris Wong的回答之外,我想举例说明有多个方法和行动的多个参数:

angular
  .module('thingApp')
  .factory('ThingResource', ['$resource', '$state',  returnThing]);
Run Code Online (Sandbox Code Playgroud)

和资源:

function returnThing($resource, $state) {
  var mainUrl = '/api/stuffs/:stuffId/thing'
  var params = {stuffId: '@_id', thingMongoId: '@_id', thingNumber: '@_id'}
  return $resource(mainUrl, params, {
    'save': {
      url: '/api/stuffs/:stuffId/thing/:thingMongoId',
      method: 'POST',
      interceptor: {
        responseError: function(e) {
          console.warn('Problem making request to backend: ', e)
          $state.go('oops')
        }
      }
    },
    'get': {
      url: '/api/stuffs/:stuffId/thing/:thingMongoId',
      method: 'GET',
      interceptor: {
        responseError: function(e) {
          console.warn('Problem making request to backend: ', e)
          $state.go('oops')
        }
      }
    },
    'assignThing':{
      method: 'POST',
      url: '/api/stuffs/:stuffId/thing/assign/:thingNumber'
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

这提供了3种不同的方法:

// POST to http://currnt_base_url/api/stuffs/:stuffId/thing/:thingMongoId
ThingResource.save({
    stuffId:'56c3d1c47fe68be29e0f7652', 
    thingMongoId: '56c3d1c47fe6agwbe29e0f11111'})

// GET to current http://currnt_base_url/api/stuffs/:stuffId/thing/:thingMongoId
ThingResource.get({
    stuffId:'56c3d1c47fe68be29e0f7652', 
    thingMongoId: '56c3d1c47fe6agwbe29e0f11111'})

// POST to http://currnt_base_url/api/stuffs/:stuffId/thing/assign/:thingNumber
ThingResource.assignThing({
    stuffId:'56c3d1c47fe68be29e0f7652', 
    thingNumber: '999998'})
Run Code Online (Sandbox Code Playgroud)