AngularJS: How do I handle RESTful APIs with token security and custom methods?

后端 未结 1 1613
野性不改
野性不改 2020-12-29 00:30

So say I have a RESTFul API that has the standard GET, POST, PUT, and DELETE methods on it, but I also h

相关标签:
1条回答
  • 2020-12-29 01:04

    If I'm understanding you correctly, what I believe you are asking is how to make the resource methods automatically include your token??? If this is correct, then you can do this a couple of ways. First, you can just extend the predefined resource methods and bake in params that will be applied each call or you can define your own methods.

    Also, when you call a method, if parameters have not been prequalified, they will end up on the querystring.

    Below is sample code I wrote for a cakephp implementation. I'm passing in action for each of the predefined methods and my own initialize method.

    
    angular.module('myApp.cakephp.services', ['ngResource']).
      factory('CommentSvc', function ($resource) {
            return $resource('/cakephp/demo_comments/:action/:id/:page/:limit:format', { id:'@id', 'page' : '@page', 'limit': '@limit' }, {
              'initialize' : { method: 'GET', params: { action : 'initialize', format: '.json' }, isArray : true },
              'save': { method: 'POST', params: { action: 'create', format: '.json' } },
              'query' : { method: 'GET', params: { action : 'read', format: '.json' } , isArray : true },
              'update': { method: 'PUT', params: { action: 'update', format: '.json' } },
              'remove': { method: 'DELETE', params: { action: 'delete', format: '.json' } } 
            });
      })
    

    hope this helps

    --dan

    0 讨论(0)
提交回复
热议问题