Angular relative paths in HTTP requests

How do relative URLs work in Angular HTTP requests? For example, I see people doing this:

$http.post('/api/authenticate', { username: username, password: password });

There is no address, only a relative URL, how does javascript running in the users browser determine which address to call? If possible, add links to material that I can read to better understand this.

+4
source share
1 answer

The base URL for this AJAX HTTP request will be the domain address in the address bar of the browser URL.

For example:

Your application runs on https://example.com/user/profileand when executed:

$http.post('/api/authenticate', { username: username, password: password });

Then the browser will execute an AJAX request to https://example.com/api/authenticate

You can get the base url from the browser using

var baseURL = window.location.protocol + '//' + window.location.host;
alert('Base URL for current frame is: ' + baseURL);
Hide result
+3

Source: https://habr.com/ru/post/1626762/


All Articles