My Angular 1.5.8 web application is at http://www.example.com/foo/
and my RESTful list of bars is at http://www.example.com/foo/api/bars
. When ui-router goes to the list of bars in Angular, my browser is at http://www.example.com/foo/#/bars
. I simply want to connect to a RESTful resource to connect to http://www.example.com/foo/api/bars
using HTTP GET
.
So I try the most obvious thing:
$resource("api/bars/")
In my mind that should resolve api/bars/
to the current path http://www.example.com/foo/#/bars
to give me http://www.example.com/foo/api/bar
, right? But it doesn't work (even though I could swear it worked with $http
). Instead it gives me a $resource:badcfg
error.
Angular lets me do the following, but this doesn't produce the correct path, instead giving me http://www.example.com/api/bars
:
$resource("/api/bars/")
So I try $browser.baseHref()
, but that seems to be set to the empty string (why?), producing http://www.example.com/api/bars
again.
$resource($browser.baseHref() + "/api/bars/")
I tried to use $location.path()
, but Angular thinks I want the path after the hash sign, so that doesn't work, giving me http://www.example.com/bars/api/bars
. Doh!
$resource($location.path() + "/api/bars/")
How can I simply resolve api/bars/
to the base path of the current URL http://www.example.com/foo/#/bars
, producing http://www.example.com/foo/api/bars
(or even just the path)?
Note that it is not acceptable for me to hard code an absolute path, or to change my HTML code. Why should I? Besides, this application will be mounted at two places on the same server (hey, it's easy with Java, Tomcat, and JAX-RS), at http://www.example.com/foo/
and http://www.example.com/foo-demo/
. The application should run unmodified under each location, accessing the RESTful API list of bars at http://www.example.com/foo/api/bars
and http://www.example.com/foo-demo/api/bars
, respectively.
This is not complicated; this is simple "URI syntax 101" which has been outlined in RFCs for over two decades. I simply want to resolve api/bars/
to the base path of the current URL http://www.example.com/foo/#/bars
. How do I do that in Angular 1.5?