Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular relative paths in HTTP requests

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.

like image 589
Tomas Avatar asked Jan 31 '16 16:01

Tomas


People also ask

What is relative path in angular?

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.

What is a relative URL 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.

What is relative path in REST API?

A relative path binds policies to a specific relative path location (for example /test/path ).

Which of these options is correct for setting headers in Angularjs HTTP POST request?

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' } .


1 Answers

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);
like image 171
Shashank Agrawal Avatar answered Oct 10 '22 18:10

Shashank Agrawal