Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the base URL of an AngularJS HTTP call?

Tags:

angularjs

My application calls $HTTP many times like this:

    this.$http({
        method: this.method,
        url: this.url
    })

The this.url is always set to something like /app/getdata

Now I have moved the back-end of my application to another server and I will need to get data like this:

https://newserver.com/app/getdata

Is there a way that I can supply a base URL that will be used for all the $http calls?

like image 805
Samantha J T Star Avatar asked Dec 05 '14 11:12

Samantha J T Star


People also ask

How to change URL path in AngularJS?

To change path URL with AngularJS, $location. path() is passed the new URL as a parameter, and the page is automatically reloaded. This means that if you ever make a change to the URL in an Angular application, then the partial and controller will be reloaded automatically.

What is base URL in angular?

You just need to put a <base> tag in the head of the index or Layout page and specify a base URL like this: <base href="/client1/" /> So if you had an Angular route defined like this: { path: 'home', component: HomeComponent }

How do you modify the $HTTP request default Behaviour?

To add or overwrite these defaults, simply add or remove a property from these configuration objects. 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.

What do you call the base URL?

Base URL: The consistent part or the root of your website's address. For example, http://www.YourDomain.com. Relative URL: The remaining path given after the base URL. For example, /contact_us, /seo/blog/new_post.


1 Answers

I found a alternative solution instead of <base> tag: written in coffeescript

$httpProvider.interceptors.push ($q)->
  'request': (config)->
    if config.url.indexOf('/api') is 0
      config.url = BASE_URL+config.url
    config
like image 88
roy Avatar answered Oct 13 '22 01:10

roy