2012-09-28 8 views
23

$リソースはすばらしいもので、Webサービスを扱うのにとても便利です。 GETとPOSTを別のURLで実行する必要がある場合はどうすればよいですか?

例えば、URLがhttp://localhost/pleaseGethere/:id でGETとPOST URLはあなたがパラメータとしてURLを公開することができるはず任意のパラメータ

答えて

32

なしhttp://localhost/pleasePosthereです。そして、あなたはGET呼び出しで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' 
       } 
      } 
     ); 
    } 
]); 

:私はこれを行うことができました。

私が本当に短いテストで見つけた1つの警告は、URL文字列にhttp://を含めると機能しなかったということでした。エラーメッセージが表示されませんでした。それは何もしなかった。その後

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

:あなたが$リソース呼び出しにPARAM名のハッシュを追加する場合

+2

問題は、urlパラメータがエンコードされているため、「http://」または「/」が付いたものです失敗するでしょう。何か案は? – Zymotik

+0

@Zymotik http://stackoverflow.com/questions/22944932/angularjs-resource-how-to-disable-url-entity-encoding – cameronroe

+2

この回答は、質問者が探していたものについてあまりにも複雑です - アイリスの答えはオンですポイント。 – btk

8

idがID機能を呼び出すのparamにマッピングされます(これは/ pleaseGethere がローカルホストGET呼び出します。/123):

Resource.get({id: 123}); 

がPOSTの場合、あなたは、単にIDのparamを割り当てません

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

適切なURLは(後続のスラッシュをngResourceによって剥離され)、この場合、POSTはlocalhost/pleaseGethereである、と呼ぶことにします。

詳細については、http://docs.angularjs.org/api/ngResource.$resource - >例 - >クレジットカードのリソースを参照してください。

53

[アクション]の[url]プロパティを使用して、デフォルトの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 
    } 
} 

使用法:http://docs.angularjs.org/api/ngResource/service/$resource

+0

"base" urlに使われている ':id'は、postMethod urlでも利用できます。 +1! –

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

は、私はそれに役立つ願っています!楽しい!アイリスウォンの答えに加えて

4

は、私は複数の方法とアクションで複数のparamsを持っていることの一例を与えたいと思った:

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'}) 
+0

は包括的かつ貴重です。 +1 –

関連する問題