简体   繁体   English

用户刷新页面时如何进行angularjs $ http POST调用?

[英]How to make angularjs $http POST call when user refresh a page?

controller: 控制器:

app.run(function ($rootScope, $templateCache) {
    $rootScope.$on('$viewContentLoaded', function ($scope, $http) {     
        $templateCache.removeAll();
        alert("refresh.....");
        $http({
            url: '/angularjs-restful/check/check-session',
            method: "POST",
            data: {},
            isArray: false
        }).success(function(data, status, headers, config){
            alert("success.....");
            console.log('status: ' + status);
            console.log('data: ' + data);
        }).error(function(data, status, headers, config){
            alert('error');
        });        
        alert("end.....");
    });       
});

I need to make an AJAX call when a user hit f5 on keyboard or manually refresh the page with a mouse. 当用户在键盘上按f5键或使用鼠标手动刷新页面时,我需要拨打AJAX电话。 How can I achieve this? 我该如何实现? the url is a rest service that either return true or false. url是一个休息服务,返回true或false。

you can use window.onbeforeunload function to listen to the unload event which should fire on f5. 您可以使用window.onbeforeunload函数来监听应该在f5上触发的unload事件。

function ctrl($scope, $window) {
    angular.element($window).bind('beforeunload', function() {
        console.log('do ajax call');
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM