使用$http请求加载新的HTML页面

Loading a new HTML page with $http request

本文关键字:HTML 页面 加载 http 请求 使用      更新时间:2023-09-26

我正在处理令牌,对于每个HTTP请求,令牌都会添加到标头中,这样服务器就可以判断用户是否登录。

因此,我无法重定向到特定的URL并检查令牌,因为令牌不会添加到标头中。

是否可以从http请求加载一个新的HTML页面?每次服务器响应时,我都会得到HTML页面的代码。我认为angular不会重新加载新的传入页面。

编辑:这是一些代码

添加到每个http请求的代码

// ===================================================
// application configuration to integrate token into requests
// ===================================================
.factory('AuthInterceptor', function($q, $location, AuthToken) {
var interceptorFactory = {};
// this will happen on all HTTP requests
interceptorFactory.request = function(config) {
    // grab the token
    var token = AuthToken.getToken();
    // if the token exists, add it to the header as x-access-token
    if (token) {
        config.headers['x-access-token'] = token;
    }
    return config;
};
// happens on response errors
interceptorFactory.responseError = function(response) {
    // if our server returns a 403 forbidden response
    if (response.status == 403) {
        AuthToken.setToken();
        $location.path('/login');
    }
    // return the errors from the server as a promise
    return $q.reject(response);
};
    return interceptorFactory;
});

我正在使用ui路由。我有一个前端和后端的网站。因此,当用户从前端登录时,front-end.html会消失,back-end.html就会被加载。但angular只是读取back-end.html代码。

// function to handle login form
vm.doLogin = function() {
    vm.processing = true;
    //clear the error
    vm.error = '';
    Auth.login(vm.loginData.email, vm.loginData.password)
        .success(function (data) {
            vm.processing = false;
            // if a user successfully logs in, redirect to main application
            if (data.success)
                return $http.get('/account');
                //Here is where a user logs in and i redirect them to the backend of the site. But the response is the HTML code of the page. I want that page to load.
            else 
                vm.error = data.message;
        });
};

您正朝着错误的方向前进。您应该阅读以下内容:http://blog.thesparktree.com/post/75952317665/angularjs-interceptors-globally-handle-401-and