简体   繁体   English

单击按钮ajax时刷新用户所在的页面

[英]Refresh the page that the user is on when clicked a button ajax

I have a logout button displayed when the user logs in, now It is only on the homepage, I want to refresh the same page when the user clicks log out, I have created a function so that when logout is pressed it refreshes the same page as on refresh it logs user out, But it doesn't refresh when logout is click 我在用户登录时显示了一个注销按钮,现在它仅在主页上,我想在用户单击注销时刷新同一页面,我创建了一个函数,以便在按下注销时可以刷新同一页面刷新时,它将注销用户,但是单击“注销”时,它不会刷新

function logout() {
    $("#logout").live("click",function(){
        $(document).ajaxStop(function(){
            window.location.reload();
        });
    });
};

I am confused why do you want to bind to ajaxstop within body of click handler, but if you just want to reload page on click of logout button, you can just use following: 我很困惑为什么要绑定到单击处理程序主体内的ajaxstop,但是如果您只想在单击注销按钮时重新加载页面,则可以使用以下命令:

function bindToLogoutClick() {
    $("#logout").on("click",function(e){
        e.preventDefault(); //Used to restrict page posting to server if logout is a submit button.
        window.location.reload();
        return false; //Used to restrict page posting to server if logout is a submit button.
    });
};

And off course somewhere you also have to call bindToLogoutClick() function to set the binding to button's click event like right on DOM load: 当然,在某些地方,您还必须调用bindToLogoutClick()函数来设置对按钮的click事件的绑定,就像在DOM加载时一样:

$(function() {
   bindToLogoutClick();
});

I'm not sure if your ajaxStop callback really is being called, after the ajax call, test it using console.log or simply alert it, like this: 我不确定在ajax调用之后是否真的在调用您的ajaxStop回调,请使用console.log对其进行测试,或者只是警告它,如下所示:

$("#logout").live("click",function(){
    $(document).ajaxStop(function(){
        alert('ajaxStop callback');
        window.location.reload();
    });
});

and if it isn't called, it means you have one or multiple ajax requests in the page which are not completed. 如果未调用它,则意味着页面中有一个或多个ajax请求尚未完成。 as you know .ajaxStop() 如你所知.ajaxStop()

Register a handler to be called when all Ajax requests have completed 注册所有Ajax请求完成后要调用的处理程序

you can check your browser developer tool and look for ongoing xhr requests. 您可以检查您的浏览器开发者工具并查找正在进行的xhr请求。

  • most likely your problem is you add .ajaxStop() trigger after all Ajax requests have completed, then since there is no ongoing ajax call, it will never fire . 最有可能是您的问题是,在所有Ajax请求完成后添加.ajaxStop()触发器,然后由于没有正在进行的ajax调用,它将永远不会触发

  • the problem is if you use several advanced jQuery or other client frameworks components, it might never happen. 问题是,如果您使用几个高级jQuery或其他客户端框架组件,则可能永远不会发生。 I mean advanced components usually always use ajax requests in the background, without you even knowing. 我的意思是高级组件通常总是在后台使用ajax请求,而您甚至都不知道。

  • other possible problem could be an ajax request gone through timeout, without being completed. 其他可能的问题可能是ajax请求经过超时而没有完成。

  • you have to be careful about the global option, since: 您必须注意global选项,因为:

If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxStop() method will not fire . 如果在全局选项设置为false的情况下调用$ .ajax()或$ .ajaxSetup(), 则不会触发 .ajaxStop()方法。

for instance if you have an ajax call like this: 例如,如果您有一个像这样的ajax调用:

$.ajax({
    //...
    global: false,
    // ...
});

or 要么

$.ajaxSetup({
    ...
    global: false,
    ...
});
$.ajax(...);

your .ajaxStop() method will not fire . 您的.ajaxStop()方法将不会触发

Try something like; 尝试类似的东西;

function logout() {
  $("#logout").live("click",function(){
    $.ajax({  // do the ajax request
      url:"file.php",
      success:function(result){
        alert(result);//do something if you want or simply remove
        location.reload();
      }
    });
  });
};

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

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