dimanche 11 février 2018

Laravel .ajax post request not received

Following a simple tutorial: https://www.tutorialspoint.com/laravel/laravel_ajax.htm

  <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
  </script>

  <script>
     function getMessage(){
        $.ajax({
           type:'POST',
           url:'/getmsg',
           data:'_token = <?php echo csrf_token() ?>',
           success:function(data){
              $("#msg").html(data.msg);
           }
        });
     }
  </script>

My function in web.php is not triggered. Click the button, nothing happens. Debug the .ajax call on the page and the .ajax is triggered.

Put a breakpoint in my web.php, that is not hit:

Route::get('ajax',function(){
return view('ajax');
});
Route::post('/getmsg',function(){
$msg = "This is a simple message.";
return response()->json(array('msg'=> $msg), 200);
});

Checked the laravel.log and nothing is there.

Is there something wrong with the post request? Is there anyway to trace Laravel to see where did the request go?

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire