Mad
Mad

Reputation: 548

Angularjs Http Request change

I need to append some value to my URLs which are having /app string on the front. Because this is the app someone has developed and we need to change URLs which I mentioned above.

Here is one of example URL which I need to change.

$http.post('api/account/createAccount')

I tried below, However, it didn't work.

$httpProvider.interceptors.push(function ($q) {
                return {

                    'request': function (config) {
                        var str = config.url;
                        if(str.search("/api") > 0 ){
                            config.url = "localhost:1337" + config.url;
                            return config || $q.when(config);
                        }

                    }


                }

            });

This is included in the config section.

Upvotes: 0

Views: 180

Answers (2)

nikjohn
nikjohn

Reputation: 21850

var app = angular.module('myApp', []);

app.config(function ($httpProvider) {
     $httpProvider.interceptors.push(function () {
         return {
             'request': function (config) {
                 if(str.indexOf(`/api` > -1) {
                     config.url += "localhost:1337";
                 }
                 return config;
             }

         }
     });
 });

Try this. Not sure what you're attempting to do here with $q

Upvotes: 1

Kunso Solutions
Kunso Solutions

Reputation: 7630

You do not return config in some cases, that's wrong. You should modify your config in some particular cases, and in the rest return untouchable config.

$httpProvider.interceptors.push(function ($q) {
    return {
       'request': function (config) {
            var str = config.url;
            if (str.search("/api") > 0) {
                config.url = "localhost:1337" + config.url;
            }

            return config;
        }
    }

});

Upvotes: 0

Related Questions