sadri
sadri

Reputation: 87

Sweet alert with ajax form and javascript in laravel

In my local project I'm trying to get response from the queries. Then I'm trying to show the notification messages via sweet alert, but I have some problems. When I try to get the response, it gets an array type mean normal text like the picture below,

response return

My code is below:

My settings 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>

My 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']);
}

Upvotes: 0

Views: 2396

Answers (1)

Qh0stM4N
Qh0stM4N

Reputation: 192

try to fix; response type select json.

return response()->json(['baslik'=>'Başarılı','icerik'=>'Kayıt Başarılı','durum'=>'success']);

add datatype for ajax

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

or change succes function

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

Upvotes: 1

Related Questions