lundi 27 août 2018

AJAX POST Request not working in Laravel 5.6

While trying to use ajax request in my application using Laravel 5.6 I've found that the,

ajax request is not triggering,

My Controller code

class AjaxController extends Controller
{
    function index(Request $request)
    {
        $msg = "Sample Messgae";
        return response()->json(['msg' => $msg)]);
    }
}

The Route,

web.php

Route::post('/message','AjaxController@index');
Route::get('/sample','UserRedirectController@ajaxRedirect');

The view

    <input type="text" name="ajax" id="ajax">
    <input type="text" name="_token" value="" id="token" hidden>
    <button id="save" onclick="ajaxRequestFun();">Show</button>
    <span id="cont"></span>

Finally The script

    <script>
        $(document).ready(function(){
            $.ajaxSetup({
                headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
            });
        });
        functon ajaxRequestFun()
        {
            $.ajax({
            type : 'POST',
            url : '/message',
            data : {'_token':  },
            success:function(res){
                alert("success");
            }
            });
        }   
    </script>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire