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

标签 javascript angularjs http rootscope

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.....");
    });       
});

当用户按键盘上的 f5 或用鼠标手动刷新页面时,我需要进行 AJAX 调用。我怎样才能实现这个目标? 该 url 是一个休息服务,返回 true 或 false。

最佳答案

您可以使用window.onbeforeunload 函数来监听应在 f5 上触发的卸载事件。

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

关于javascript - 当用户刷新页面时,如何进行 angularjs $http POST 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22549407/

相关文章:

javascript - 如何防止在knockout.js自定义绑定(bind)中递归调用更新和值更改回调?

javascript - 如何直接根据用户输入更改输入框中的特定文本?

javascript - AngularJS 表单仍然 $dirty,手动设置每个输入 $pristine

javascript - Angular Material 布局 - 填充页面其余部分的 Div

java - 如何将缓存控制添加到 @RestController servlet 路径?

javascript - 在 QML 中获取 C++ 对象并在 javascript 中使用它

javascript - 带有 localStorage 变量的 jQuery 循环不起作用

javascript - 如何在 AngularJs 中使用正则表达式和 ng-repeat?

http - 具有非常大的随机数的安全 URL

rest - 如何在不存在 Accept header 时执行内容协商