Using a Relative Path for a Service Call in AngularJS

后端 未结 9 2070
别跟我提以往
别跟我提以往 2020-11-28 03:51

I have the following code, which was working fine until I deployed to a test server:

$scope.getUserList = function (userName) {
    $http({
        method: \         


        
9条回答
  •  [愿得一人]
    2020-11-28 04:13

    I wasn't able to use tag since my application was created in a popup dynamically from another origion. I was considering the others option in this thread to use something like a basePath variable and use it in every $http, ng-src, templateUrl etc

    But that was a bit overhead, built a interceptor that change every url before a xhr is made

    var app = angular.module("myApp", []);
    
    app.config(["$httpProvider", function($httpProvider) {
        $httpProvider.interceptors.push('middleware');
    }]);
    
    app.factory('middleware', function() {
        return {
            request: function(config) {
                // need more controlling when there is more than 1 domain involved
                config.url = "//example.com/api/" + config.url
                return config;
            }
        };
    });
    
    app.controller("Ctrl", ["$http", function($http) {
        $http.get("books"); // actually requestUrl = http://example.com/api/books
    }])
    

    And html aswell

    But i do recommend to use tag instead unless you are using window.popup()

提交回复
热议问题