【问题标题】:Sweet Alert is not working inside ajax calls in djangoSweet Alert 在 django 的 ajax 调用中不起作用
【发布时间】:2021-01-14 17:08:24
【问题描述】:

甜蜜警报 - https://sweetalert.js.org/guides/

我已经在我的基本 html 文件中使用 CDN 导入了 Sweet Alert:

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

我尝试通过以下方式在 ajax 调用中使用 Sweet Alert:

$.ajax({
            type:'POST',
            url: 'deliveryupdate/'+barcode2+'/',
            dataType: 'json',
            data:{
                barcode: barcode2,
                owner: owner2,
                mobile: mobile2,
                address: address2,
                atype: atype2,
                status: "Gecia Authority Approved",
                statusdate: today,
                csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
            },
            success: function(){
              console.log("success log");
              swal("Success!","Asset request has been approved","success");
            },
            error: function(){
              console.log('error', arguments)
              swal("Error!","Some error occurred","error");
            }
        });

这不起作用,即使数据库中有成功的更改,正在执行错误函数而不是成功函数,并且错误甜蜜警报会闪烁一分钟然后消失。控制台显示错误日志。

但是,如果我将甜蜜警报更改为普通浏览器警报,它会完美运行。(但这也仅在 Chrome 中,而不是在 firefox 中)

当我用 alert() 替换 swal() 时它工作得很好。 (虽然仅在 Chrome 中)

我不想要普通的浏览器警报,我需要一个好看的警报,比如 Sweet Alert。在我的其他没有 ajax 调用和简单警报的模板中,sweet alert 工作正常,没有任何问题。

请帮忙。

编辑:添加视图功能:

def gecia_ass_del(request):
    deliverylist = Delivery.objects.filter(status='Added to Delivery List')
    context = {'deliverylist': deliverylist}
    return render(request, 'gecia_ass_del.html', context)

只是为了清楚视图中的上下文与我的 ajax 调用无关。

【问题讨论】:

  • 请提供views 来响应您的ajax 调用。
  • 听起来您是在表单事件侦听器中发出请求,而不是阻止默认表单提交
  • @BiploveLamichhane 我已在问题中添加。
  • @charlietfl 我有 2 个按钮 - 批准和拒绝。根据单击的按钮,我正在执行不同的 ajax 调用。我没有这样的表单提交按钮。

标签: javascript jquery django alert sweetalert


【解决方案1】:

你必须用 Swal.fire 替换回调函数 swal

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多