【问题标题】:SweetAlert 2 and Internet ExplorerSweetAlert 2 和 Internet Explorer
【发布时间】:2020-05-06 15:53:22
【问题描述】:

所以基本上我们在我们的网站上使用 SweetAlert2。 除了 Internet Explorer,一切都在 Chrome、Firefox、Edge 等各种网络浏览器上运行良好。

起初 - 几乎每个人都知道 - SweetAlert2 使用的是 Internet Explorer 不支持的箭头功能。然而,我们也知道——Internet Explorer 不支持箭头函数,所以我们决定用经典函数覆盖箭头函数。

发件人:

Swal.fire({
    title: 'Success',
    html: message,
    icon: 'success'
    }).then((result) => {
        window.location.reload();
});

我们做到了:

Swal.fire({
    title: 'Success',
    html: message,
    icon: 'success'
    }).then(function (result) {
        window.location.reload();
});

是的,上面的代码使它可以在 Internet Explorer 上运行。我们很高兴,直到我们意识到这种代码也会导致 AJAX POST 请求出现问题,因此应用程序中的几乎所有功能都停止工作。

否则 - 如果我们在 SweetAlert2 调用方法中切换回箭头函数 - 那么我们所有的 ajax 发布请求都可以正常工作。

@edit => 更多场景信息

所以在 ajax 调用之后 - 发布数据正在发送到 php 文件,该文件检查是否使用 isset() 方法发送了发布数据。如果是,则查询数据库,否则返回错误代码。

在我们使用箭头函数的场景中 - isset() 检查发布数据返回 true 并且有一个查询。

在我们不使用箭头函数的情况下 - isset() 检查发布数据返回 false 并且 ajax 调用返回错误代码。

所以看起来表单中的帖子数据没有被发送。

@edit2 - 在下面的链接中添加js函数代码

$('.form-ajax').on('submit', function(event) {

    event.preventDefault();

    var link = $(this).attr('action');
    var redirect = false;
    var callback = false;

    // Sweet Ajax
    var confirm_question = false;
    var success_msg = false;

    if ($(this).attr('redirect')    !== undefined)      { redirect = $(this).attr('redirect'); }
    if ($(this).attr('callback')    !== undefined)      { callback = $(this).attr('callback'); }

    // Sweet Ajax
    if ($(this).attr('confirm')         !== undefined) { confirm_question = $(this).attr('confirm'); }
    if ($(this).attr('success')         !== undefined) { success_msg = $(this).attr('success'); }


    if (confirm_question !== false) {

        // Pytanie zostało określone
        Swal.fire({
            title: 'Potwierdzenie',
            html: confirm_question,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Potwierdź',
            cancelButtonText: 'Anuluj'
            }).then(function (result) {
            if (result.value) {

                $.ajax({
                    url: link,
                    type: 'POST',
                    data: $(this).serialize(),
                    success: function(result) {

                        if (result == 'ok') {

                            var komunikat = 'Operacja została wykonana poprawnie.';

                            Swal.fire({
                                title: 'Sukces',
                                html: komunikat,
                                icon: 'success'
                            }).then(function (result) {
                                if (redirect) {
                                    window.location.replace(redirect);
                                } else {
                                    window.location.reload();
                                }
                            });

                        } else {

                            if (result == '405') {

                                if (callback) {
                                    $('#' + callback).html('<i class="fas fa-exclamation-circle"></i> Wystąpił błąd1. Odśwież stronę i spróbuj ponownie.');
                                } else {

                                    // swal fire
                                    Swal.fire({
                                        title: 'Błąd',
                                        html: 'Wystąpił błąd. Odśwież stronę i spróbuj ponownie.',
                                        icon: 'error'
                                    });

                                }

                            }
                        }
                    }
                });

            }
        });

    }

});

上述函数返回“Wystąpił błąd1. Odśwież stronę i spróbuj ponownie”。如果我们不使用箭头函数,这意味着结果将返回 405。

我们被困住了,不知道我们还能尝试什么?

【问题讨论】:

  • 我看不出上述更改会如何阻止 ajax 代码工作?
  • @ChrisG 在来自 js 的 ajax 请求之后 - php 文件正在检查 $post['name'] 是否已设置,如果未设置则返回错误代码,否则它会查询数据库.当使用箭头函数时 - 检查返回 true 否则返回 false 所以整个 ajax 调用错误代码。
  • 你能在问题中添加相关的ajax代码吗?我仍然不明白这两者有什么关系,但我们需要查看 ajax 代码来调试它。
  • @ChrisG 当然,刚刚添加了代码
  • 那是一堵代码墙。请创建一个minimal reproducible example

标签: javascript internet-explorer sweetalert2


【解决方案1】:

好的,伙计们 - 所以基本上我找到了一个适合我的解决方案。

我做了一个简单的 PHP 检查用户是否使用 Internet Explorer 或任何其他网站。视情况而定 - 脚本正在为 Internet Explorer 加载 JS(实际上不使用箭头函数)或为其他网站加载 JS(实际上使用箭头函数)。

<?php 

// check user's agent
preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches);

if(count($matches)<2){
    preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches);
}

if (count($matches)>1){

    // yes, user is using IE => load JS for IE

    ?>
        <script type="text/javascript" src="javascript-ie.js"></script>
    <?php

} else {

    // no, user is using web browser other than IE => load default JS

    ?>
        <script type="text/javascript" src="javascript.js"></script>
    <?php

} ?>

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 2012-08-31
    • 2018-12-14
    • 2013-01-06
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多