lundi 25 juillet 2016

Getting 500 ERROR when doing an AJAX request in Laravel

I'm new to Laravel. I'm trying to make an AJAX request in my laravel app but I'm getting a 500 (Internal Server Error). So, here is my request in the .blade file:

    <script>
      $(document).ready(function () {
        $.ajaxSetup({
          headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $('#getRequest').on('click', function () {
            $.get('getMessages', function (data) {
               $('#target').append(data);
            });
       });
     });
   </script>

I added the .ajaxSetup to make sure tokens are not the reason for this problem. So I typed in this .blade file also the following tag:

<meta name="csrf-token" content="" />

Here is my route.php file:

Route::get('getMessages', 'PagesController@getMessages');

And here is my controller with the method in cause:

public function getMessages()
{
    return "OK";
}

The problem is tricky to me, because I know that I can create a anonimous function in my route.php file for this URI and it's gonna be the same thing. Or not. I don't know because if I actually do this

Route::get('getMessages', function ()
{
    return "OK";
});

instead of pointing to a method of a controller, it works! But I need it to work in a controller.

My controller is functioning properly when it comes to other methods and the name of the method is spelled correctly everywhere.

I'm working with XAMPP on Windows. I set XAMPP to work only with the current Laravel app, so when I type in "localhost" in my browser, it gets me to my app page and all of database data fetching work properly.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire