2012-09-28 24 views
23

$ tài nguyên là tuyệt vời cung cấp cách thuận tiện để xử lý các dịch vụ web. Điều gì xảy ra nếu GET và POST phải được thực hiện trên các URL khác nhau?

Ví dụ, GET URL là http://localhost/pleaseGethere/:id và POST URL là http://localhost/pleasePosthere mà không cần bất kỳ thông số

Trả lời

32

Bạn sẽ có thể để lộ địa chỉ URL như một tham số. Tôi đã có thể thực hiện điều này:

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

Sau đó, bạn có thể ghi đè URL đó vào cuộc gọi GET.

Thông báo trước tôi tìm thấy trong thử nghiệm ngắn gọn thực sự của tôi là nếu tôi bao gồm http:// trong chuỗi URL, nó không hoạt động. Tôi không nhận được thông báo lỗi. Nó không làm gì cả.

+2

Vấn đề là người gửi url được mã hóa, đó là lý do 'http: //' hoặc bất kỳ thứ gì có '/' sẽ thất bại. Bất kỳ ý tưởng? – Zymotik

+0

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

+2

Câu trả lời này hơi phức tạp cho những gì người hỏi đang tìm kiếm - câu trả lời của Iris đang bật điểm. – btk

8

Nếu bạn thêm băm với tên param vào cuộc gọi $ tài nguyên:

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

Sau đó: id sẽ được ánh xạ tới id param khi gọi chức năng (điều này sẽ gọi GET localhost/pleaseGethere/123):

Resource.get({id: 123}); 

Đối với POST, bạn chỉ đơn giản là không giao id param:

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

URL thích hợp sẽ được gọi, trong trường hợp này là POST localhost/pleaseGethere (dấu gạch chéo sau bị tước bởi ngResource).

Xem http://docs.angularjs.org/api/ngResource.$resource -> Ví dụ -> Tài nguyên thẻ tín dụng để biết thêm chi tiết.

53

Sử dụng thuộc tính 'url' của [actions] để ghi đè url mặc định.

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

ví dụ:

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

Sử dụng tài nguyên $ kiễu góc: http://docs.angularjs.org/api/ngResource/service/$resource

+0

Đó là ': id' được sử dụng trong url" cơ sở "cũng có sẵn trong url postMethod. +1! –

1

Làm theo cách này:

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

Tôi hy vọng nó giúp đỡ! Thưởng thức!

4

Ngoài câu trả lời Iris Wong, tôi muốn đưa ra một ví dụ về việc có nhiều params với nhiều phương pháp và hành động:

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

Và tài nguyên:

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

Mà cho 3 phương pháp riêng biệt :

// 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

toàn diện và có giá trị. +1 –

Các vấn đề liên quan