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

时间:2012-09-28 05:06:45

标签: angularjs

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

例如,GET网址为http://localhost/pleaseGethere/:id 和POST网址为http://localhost/pleasePosthere,没有任何参数

5 个答案:

答案 0 :(得分:55)

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

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

例如:

$resource('http://localhost/pleaseGethere/:id',{},{
    getMethod:{
        method:'GET',
        isArray:true
    }
    postMethod:{
        url:'http://localhost/pleasePosthere',
        method:'POST',
        isArray:false
    }
}

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

答案 1 :(得分: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'
                }
            }
        );
    }
]);

然后,您可以覆盖GET来电时的网址。

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

答案 2 :(得分:8)

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

$resource('localhost/pleaseGethere/:id', {id: '@id'});

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

Resource.get({id: 123});

对于POST,您只是不指定 id 参数:

Resource.post({}, {name: "Joe"});

将调用正确的URL,在这种情况下, POST localhost / pleaseGethere (尾部斜杠被ngResource剥离)。

请参阅http://docs.angularjs.org/api/ngResource.$resource - >示例 - >信用卡资源了解更多详情。

答案 3 :(得分:5)

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

angular
  .module('thingApp')
  .factory('ThingResource', ['$resource', '$state',  returnThing]);

资源:

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

这提供了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'})

答案 4 :(得分:1)

按照这种方式:

(function () {
    'use strict';

    angular
        .module("app")
        .factory("SomeFactory", SomeFactory);

    function SomeFactory($resource) {
        var provider = "http://stackoverflow.com/:action/:id";
        var params = {"id":"@id"};
        var actions = {
            "create":   {"method": "POST",  "params": {"action": "CreateAwesomePost"}},
            "read":     {"method": "POST",  "params": {"action": "ReadSomethingInteresting"}},
            "update":   {"method": "POST",  "params": {"action": "UpdateSomePost"}},
            "delete":   {"method": "GET",   "params": {"action": "DeleteJustForFun"}}
        };

        return $resource(provider, params, actions);
    }
})();

我希望它有所帮助!享受!