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.
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.
Share Improve this question edited Feb 1, 2016 at 5:25 Shashank Agrawal 25.8k11 gold badges96 silver badges125 bronze badges asked Jan 31, 2016 at 16:49 TomasTomas 1,2624 gold badges16 silver badges26 bronze badges2 Answers
Reset to default 26The 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./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./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);
Just prepend "./"
to the path you want to make relative
$http.post('./api/authenticate', { username: username, password: password });