问题与jquery ajax和谷歌浏览器 [英] Problem with jquery ajax and google chrome

查看:118
本文介绍了问题与jquery ajax和谷歌浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var changes =使用jQuery在离开页面时显示用户的确认信息。假; 

window.onunload = function(){
if(changes){
$ .post(check.php,{undovideokey:VID});
} else返回null;
};

另一方面

 < input type ='submit'name ='Add'value ='submit'align ='right'onclick =changes = true; /> 

刷新页面时,在Google Chrome中运行代码时会出现问题。我看到过很多这样的问题,但没有帮助。

在onunload处理程序触发后的jQuery ajax调用在获取手动刷新页面之后调用onunload处理程序触发on-on-a-manual-re。如何保证onunload首先发生?



如果我理解正确,那么您的问题是页面已被刷新之后发生AJAX请求。



更改要在 onbeforeunload 事件上触发的代码并使用同步AJAX请求应解决您的问题:

  var changes = false; 

window.onbeforeunload = function(){
if(changes){
$ .ajax({
async:false,
url:check .php,
data:{
undovideokey:'foo'
}
});
} else {
return null;
};
};

请注意,在Chrome的开发人员工具中很难看到它,因为它不会在重新加载时持久保留AJAX请求。您应该验证服务器端的行为或使用提琴手之类的工具。



演示: http://jsfiddle.net/wq4hk/4/show(可通过 http://jsfiddle.net/wq4hk/4/ 进行编辑)


I'm using jQuery to show a confirmation message for the user when leaving the page as follows:

var changes = false;  

window.onunload =function() { 
    if (changes) {
        $.post("check.php",{undovideokey:VID});
    } else return null; 
};

on the other hand

<input type='submit' name='Add' value='submit ' align='right' onclick="changes = true;" /> 

A problem occurs when running the code in Google Chrome when I refresh the page. I have seen many problems like this but there is not helpful answers.

jQuery ajax call in onunload handler firing AFTER getting the page on a manual refresh. How do I guarantee onunload happens first?

window.onbeforeunload ajax request in Chrome

thanks, Sara

解决方案

If I understand correctly, your issue that the AJAX request occurs after the page has been refreshed.

Changing the code to trigger on the onbeforeunload event and using a synchronous AJAX request should fix your problem:

var changes = false;

window.onbeforeunload = function() {
    if (changes) {
        $.ajax({
            async: false,
            url: "check.php",
            data: {
                undovideokey: 'foo'
            }
        });
    } else {
        return null;
    };
};

Note that this is difficult to see in Chrome's developer tools, since it does not persist AJAX requests across reloads. You should verify the behaviour on the server-side or by using a tool like Fiddler

Working demo: http://jsfiddle.net/wq4hk/4/show (Editable via http://jsfiddle.net/wq4hk/4/)

这篇关于问题与jquery ajax和谷歌浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆