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

时间:2016-01-13 18:33:07

标签: javascript html angularjs

我正在处理令牌,并且每个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-routing。我有一个网站的前端和后端。因此,当用户从前端登录时,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;
        });
};

1 个答案:

答案 0 :(得分:0)