How do relative URLs work in Angular's HTTP requests? Example, I see people doing this:
$http.post('/api/authenticate', { username: username, password: password });
There is no address here, just a relative URL, how can javascript, running in a users browser, figure out which address to call? If possible please add links to material I can read to better understand this.
So by the use of relative path you are making your links free that are relative to the current URL segment. Your feature area of routing will be the same even if you change the route for the parent. You are just making your link free even if you change the parent route path.
A relative URL is a URL that only includes the path. The path is everything that comes after the domain, including the directory and slug. Because relative URLs don't include the entire URL structure, it is assumed that when linking a relative URL, it uses the same protocol, subdomain and domain as the page it's on.
A relative path binds policies to a specific relative path location (for example /test/path ).
To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults. headers. get = { 'My-Header' : 'value' } .
The base URL for this HTTP AJAX request will be the domain address in the URL address bar in the browser.
For example:
Your application is running on https://example.com/user/profile
and when you execute:
$http.post('/api/authenticate', { username: username, password: password });
Then the browser will make an AJAX request to https://example.com/api/authenticate
You can get the base URL from browser using
var baseURL = window.location.protocol + '//' + window.location.host;
alert('Base URL for current frame is: ' + baseURL);
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