My api requires a trailing slash for api calls. I was wondering how to pull that off with angular.
So i need to be able to access /tasks/
or a /tasks/xxxx/.
I attempted to do it via:
angular.module('taskServices', ['ngResource']). factory('Tasks', function($resource){ return $resource('/tasks/:task_id/', {}, { query: {method:'GET', params:{}, isArray:true} }); });
and a
$scope.tasks = Tasks.query();
But it results in a /tasks
or a tasks/xxx
query.
How can i force it to always be /tasks/
and /tasks/xxx/
Always use trailing slashes for any resource that may contain children. Just consider "GET" on a public_html directory with files.
A 301 redirect is the best way to resolve duplicate content issues caused by trailing slashes. If you're just fixing one page, you'd redirect the duplicate copy to the version that matches your chosen URL structure. Most trailing slash issues however, affect many pages across a website.
Historically, it's common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file: http://example.com/foo/ (with trailing slash, conventionally a directory) http://example.com/foo (without trailing slash, conventionally a file)
Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.
This seems to have been fixed: https://github.com/angular/angular.js/pull/5560
You can now configure it this way:
app.config(function($resourceProvider) { $resourceProvider.defaults.stripTrailingSlashes = false; });
The trailing slash is explicitly removed in this line of AngularJS source code. I'm not fully sure what was the rationale behind this code but there is already an issue opened for this: https://github.com/angular/angular.js/issues/992
As noted in the mentioned issue the $resource
factory is great for RESTful endpoints that conform to a certain specification. While $resource
will do do great job talking to back-ends conforming to a this specification, it has limitations that might rule it out for back-ends that don't obey a contract expected by the $resource
. In such a case the best way forward is to switch to using the lower-level $http
service as noted in this question: Recommended way of getting data from the server
$http
is a very powerful and flexible service and allows full control over URLs, parameters sent etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With