最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular relative paths in HTTP requests - Stack Overflow

programmeradmin6浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 26

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./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 });

发布评论

评论列表(0)

  1. 暂无评论