Angularjs Http请求更改

时间:2016-10-19 11:16:42

标签: angularjs http angular-promise

我需要为前面有/app字符串的网址添加一些值。因为这是某人开发的应用程序,我们需要更改上面提到的URL。

以下是我需要更改的示例网址之一。

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

我在下面试过,但是,它没有用。

$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);
                        }

                    }


                }

            });

这包含在配置部分中。

2 个答案:

答案 0 :(得分:1)

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;
             }

         }
     });
 });

试试这个。不确定您在$q

尝试做什么

答案 1 :(得分:0)

在某些情况下,您不会返回配置,这是错误的。您应该在某些特定情况下修改您的配置,并在其余部分返回不可触及的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;
        }
    }

});