Samir Paruthi
Samir Paruthi

Reputation: 121

Unable to Get data from ajax in laravel

Unable to get data from ajax in laravel. Here is my code for all my files which I am using

-------- My view from where I am getting this function call-----

<input type="date" min="" class="form-control" id="checkoutdate" name="checkoutdate" onchange="checkdate()" required>


and my if part is working fine 


------- Custom.js------------- Where I wrote my own javascript

    function checkdate()
    {
        var checkindate = document.getElementById("checkindate").value;
        var checkoutdate = document.getElementById("checkoutdate").value;

        if(checkindate> checkoutdate)
        {
            alert("Checkout date error please correct it");
            document.getElementById('checkoutdate').value = checkindate;
            stop();
        }
        else
        {
            $.ajax({
                url: '/checkavailability',
                type: 'POST',
                dataType: 'json',
                data:{checkin: $('#checkindate').val(), checkout: $('#checkoutdate').val()}, // the value of input having id vid
                success: function(response){ // What to do if we succeed
                    alert($arr);
                }
            });
        }

-------Route I defined------------

    Route::post('/checkavailability','BookingController@check');

-----------My controller function-------

 public function check(Request $request)
    {
       $data = $request->all();
        $checking = DB::table('bookings')->get();
        $arr = "first sucess request";
        return Response()->json($arr);
    }

check function is just for checking but it is not working

in my browser console, it gives an error for Failed to load resource: the server responded with a status of 419 (unknown status)

Upvotes: 2

Views: 2051

Answers (4)

Zain Farooq
Zain Farooq

Reputation: 2964

419 error happens when you don't post csrf_token. in your post method you must add this token along other variables. Add csrf_token in the ajax request. Hope it will fix the error

How to add csrf token in your ajax request?

First add this meta tag in your master layout

 <meta name="csrf-token" content="{{ csrf_token() }}">

Then you can get csrf token from this meta tag in your ajax request

 var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        var name = $("#name").val();
        $.ajax({
            url: '/checkavailability',
            type: 'POST',
            dataType: 'json',
            data:{_token: CSRF_TOKEN, checkin: $('#checkindate').val(), checkout: $('#checkoutdate').val()}, // the value of input having id vid
            success: function(response){ // What to do if we succeed
                alert($arr);
            }
        });

I hope it will help you.

Upvotes: 2

zahid hasan emon
zahid hasan emon

Reputation: 6233

419 error is missing CSRF token. while using a post request you have to send a csrf token.

$.ajax({
    url: '/checkavailability',
    type: 'POST',
    dataType: 'json',
    data:{_token: {{ csrf_token() }}, checkin: $('#checkindate').val(), checkout: $('#checkoutdate').val()}, 
    success: function(response){ // What to do if we succeed
        alert($arr);
    }
});

Upvotes: 2

Joyal
Joyal

Reputation: 2691

You have to pass csrf_token along with the POST request

see following code, first data value is csrf token

    $.ajax({
        url: '/checkavailability',
        type: 'POST',
        dataType: 'json',
        data:{"_token":"{{ csrf_token() }}", checkin: $('#checkindate').val(), checkout: $('#checkoutdate').val()}, // the value of input having id vid
        success: function(response){ // What to do if we succeed
            alert($arr);
        }
    });

Upvotes: 1

farooq
farooq

Reputation: 1673

You probably missing the csrf_token in your view file . Add the following in the head tag at the top of your view file .

<meta name="csrf-token" content="{{ csrf_token() }}">

And also don't forget to add the lines in your ajax request to send the csrf_token

$.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

Upvotes: 3

Related Questions