简体   繁体   English

Sweet Alert 在 Django 中的 ajax 调用中不起作用

[英]Sweet Alert is not working inside ajax calls in django

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

I have imported Sweet Alert using CDN inside my base html file:我在我的基本 html 文件中使用 CDN 导入了 Sweet Alert:

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

I have tried to use Sweet Alert inside ajax calls in the following way:我尝试通过以下方式在 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");
            }
        });

This is not working, even though there are successful changes in the database, the error function is executing instead of success function, and the error sweet alert flashes for a mini-second and vanishes.这不起作用,即使数据库中有成功更改,错误函数正在执行而不是成功函数,并且错误甜蜜警报闪烁一分钟并消失。 The console shows error log.控制台显示错误日志。

But if I change sweet alert to normal browser alert, it works perfectly.( but that too only in Chrome, not in firefox )但是,如果我将甜蜜警报更改为普通浏览器警报,它就可以完美运行。(但这也仅在 Chrome 中,而不是在 firefox 中)

Its working perfectly when I replace swal() with alert().当我用 alert() 替换 swal() 时,它工作得很好。 (only in Chrome though) (虽然仅在 Chrome 中)

I don't want the normal browser alert, I need a good looking alert like Sweet Alert.我不想要普通的浏览器警报,我需要一个像 Sweet Alert 这样好看的警报。 In my other templates where there are no ajax calls and simple alerts, sweet alert is working fine with no problems.在我的其他模板中,没有 ajax 调用和简单警报,sweet alert 运行良好,没有任何问题。

Please Help.请帮忙。

edit: Adding views function:编辑:添加视图功能:

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)

Just to be clear the context in the views is not related to my ajax calls.只是要清楚视图中的上下文与我的 ajax 调用无关。

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

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

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