javascript - AJAX 'success' 在 IE 中 AJAX 执行之前调用

标签 javascript php jquery ajax internet-explorer

简要说明

要添加到列表中的另一个 Internet Explorer 问题...我的页面上有一个关闭当前选项卡/窗口的按钮,但在执行此操作之前它会执行 AJAX 请求以“删除文档 session ”。非常简单的东西:

MN.ajax({
    url: url,
    dataType: "JSON",
    errorText: 'There was a problem deleting the session, please retry later',
    success: function(data){
        // Close the tab/window
        window.open(window.location, '_self').close();
    }
});

忽略MN.ajax(),这只是$.ajax()的包装函数,它的工作原理相同,但使用一些额外的位,例如“errorText”

问题

以上代码适用于除 IE 之外的所有浏览器。在 IE 中,它会在 AJAX 请求实际执行之前关闭窗口/选项卡。 AJAX 请求应该从我的数据库中删除一条记录,但除非我注释掉该行 window.open(window.location, '_self').close();

,否则不会删除任何记录

可能的解决方案

我还没有对此进行测试,但我认为如果我用 setTimeout 包装关闭窗口,那么它应该可以工作。不过,我想知道的是为什么!!到底为什么它会在 AJAX 请求实际完成之前执行“成功”函数?

我尝试过...

我尝试过的另一件事是将“关闭窗口”代码放入 complete 回调中,而不是 success 回调中,但这绝对没有区别。 ..

更新

这是完整的包含函数:

/**
 * Close the Document
 */
MN.closeDocument = closeDocument;
function closeDocument(document_reference,session_id){
    // Show the loading dialog
    MN.loadingDialog('Closing Document...',function(){
        // Execute the autosave function to save the session
        MN('#document-modify-form').autosave('save',function(){
            MN.ajax({
                url: '//api.example.net/document/session/'+document_reference+'/',
                dataType: "JSON",
                errorText: 'There was a problem getting the documents session',
                success: function(data){
                    // Only close if there are no changes
                    switch(data.status){
                        // There is no session or no changes
                        case 'no_session':
                        case 'no_changes':
                            // Delete the session
                            MN.ajax({
                                url: '//api.example.net/document/delete-session/'+document_reference+'/',
                                dataType: "JSON",
                                errorText: 'There was a problem deleting the session, please retry later',
                                success: function(data){
                                    // Close the tab/window
                                    window.open(window.location, '_self').close();
                                },
                                error: function(msg){
                                    // Error code here
                                }
                            });
                        break;
                        // Other cases here
                    }
                },
                error: function(msg){
                    // Error code here
                }
            });
        });
    });
}

最佳答案

我根据您的代码构建了以下简化的测试页面。这在我的系统(Win8.1 x64、IE11)上按预期工作:当 ajax 调用完成时,窗口将在 3 秒后关闭。

ajax.php:

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
sleep(3);
?>
<!DOCTYPE html>
<html>
<head>
    <title>ajax test</title>
    <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
        function test_click()
        {
            $.ajax({
                url: 'ajax.php',
                success: function(data)
                {
                    // Close the tab/window
                    window.open(window.location, '_self').close();
                }
            });
        }
    </script>
</head>
<body>
    <a href="#" onclick="test_click()">Close</a>
</body>
</html>

关于javascript - AJAX 'success' 在 IE 中 AJAX 执行之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755646/

相关文章:

javascript - 基于鼠标移动的 HTML5 平移

javascript - 有没有简单的方法可以找到 jQuery 代码的 "javascript equivalent"?

php - 使用 php 运行外部程序

javascript - 我的代码没有从 tinymce.min.js 加载文本区域

jquery - 如何使用 ajax(或简单的,以防 ajax 不可能)从网站到我的网站的结果?

javascript - 我无法在 jQuery 绑定(bind)的回调函数中返回任何内容

javascript - 我可以 "Like"Sharepoint 中的一个页面而不打开它吗?

javascript - typescript 装饰器不适用于箭头功能

javascript - 如何显示在 iframe 中呈现的 html 文件的源代码

PHP in_array() 总是返回 false