javascript - Angular JS: How to set context path for all calls through $http? - Stack Overflow

I'm deploying my Angular application in a web server, where the app may live at http:localhost:8

I'm deploying my Angular application in a web server, where the app may live at http://localhost:8080/app-name, or /, or some other URL. I do not have any guarantees about the absolute path - sometimes the call may need to go to /app-name/foo, sometimes to /foo, sometimes to /foobars/foo. In other words, there is a moving part: /app-name, (empty), /foobars in samples above. Also known as context path.

I know I can use <base> for HTML. I can wrap $http or configure Restangular to prepend context path for all calls.

But there still are cases that I don't yet know how to address. For instance, whenever directive uses a template, it is loaded via bare $http as well.

How can I get $http to always use some specific base path for all requests?

I'm deploying my Angular application in a web server, where the app may live at http://localhost:8080/app-name, or http://foobars./, or some other URL. I do not have any guarantees about the absolute path - sometimes the call may need to go to /app-name/foo, sometimes to /foo, sometimes to /foobars/foo. In other words, there is a moving part: /app-name, (empty), /foobars in samples above. Also known as context path.

I know I can use <base> for HTML. I can wrap $http or configure Restangular to prepend context path for all calls.

But there still are cases that I don't yet know how to address. For instance, whenever directive uses a template, it is loaded via bare $http as well.

How can I get $http to always use some specific base path for all requests?

Share Improve this question asked Oct 9, 2013 at 20:34 Konrad GarusKonrad Garus 54.1k44 gold badges162 silver badges233 bronze badges 1
  • According to the docs you can add a request transform to modify the request configuration object. Unfortunately, I couldn't find too much info describing the request configuration object, but after taking a quick look through the source, it appears that the url being requested is ing from that config object. – Jason Commented Oct 9, 2013 at 20:57
Add a ment  | 

1 Answer 1

Reset to default 2

You will want to configure the $httpProvider to use an Interceptor that modifies the config that the call to $http will use.

See the Interceptors section here: http://docs.angularjs/api/ng.$http

This will allow you to control your contextual path in any way you need for all $http calls.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745465624a4628904.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信