与ajax形式和javascript在laravel的甜警报

时间:2017-10-07 00:17:50

标签: laravel sweetalert

在我的本地项目中,我试图从查询中获得响应。然后我试图通过甜蜜警报显示通知消息,但我有一些问题。当我尝试获得响应时,它获得的数组类型意味着普通文本,如下图所示,

response return

我的代码如下:

我的设置blade.php

<script src="/js/jquery.validate.min.js"></script>
<script scr="/js/jquery.form.min.js"></script>
<script src="/js/messages_tr.min.js"></script>
<script src="/js/sweetalert2.min.js"></script>
<script>
    $(document).ready(function () {
        $('form').validate();
        $('form').ajaxForm({
            beforeSubmit : function(){
            },
            success:function (response) {
                    swal(
                        response.baslik,
                        response.icerik,
                        response.durum

                    )

            }
        });
    });
</script>

我的controller.php

$ayarlar = ayarlar::where('id',1)->update($request->all());
if($ayarlar) {
return response(['baslik'=>'Başarılı','icerik'=>'Kayıt 
Başarılı','durum'=>'success']);
}else {
return response(['baslik'=>'Başarılı','icerik'=>'Kayıt 
Başarılı','durum'=>'error']);
}

1 个答案:

答案 0 :(得分:1)

尝试修复;响应类型选择json。

return response() - &gt; json(['baslik'=&gt;'Başarılı','icerik'=&gt;'Kayıt Başarılı”, '硬粒'=&GT; '成功']);

为ajax添加数据类型

$('form').ajaxForm({
    beforeSubmit : function(){},
    dataType : "json",
    success:function (response) {
        swal(
            response.baslik,
            response.icerik,
            response.durum
        );
    }
});

或更改成功功能

success:function (response) {
    response = JSON.parse(response);
    swal(
        response.baslik,
        response.icerik,
        response.durum
    );
}