AngularJS - $resource different URL for Get and Post

前端 未结 5 1466
南笙
南笙 2020-12-24 14:35

$resource is awesome providing very convenient way to handle web services. What if GET and POST have to be performed on different URLs?

For example, GET URL is

5条回答
  •  有刺的猬
    2020-12-24 14:59

    You should be able to expose the URL as a parameter. I was able to do this:

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

    Then you can overwrite the URL on your GET call.

    The one caveat I found during my REALLY brief testing was that if I included http:// in the URL string, it didn't work. I didn't get an error message. It just did nothing.

提交回复
热议问题