Mad Mad - 1 month ago 18
HTTP Question

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.

Answer
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