0

I got this error from Ajax call ! it's get action from checkbox then send data by Ajax to controller method,

jquery.min.js:4 POST http://localhost:8000/listhotelregisration 500 (Internal Server Error)

Here's the code html part:

    <div style="display:block">
      <div>My hotel Lists</div>
        @foreach($myLists as $key => $val)
            {{ $val['name'] }
            {!! Form::checkbox($val['name'], $val['id'].','.$value['id']) !!} <br>
        @endforeach
       </div>

Ajax part:

 $(function() {
              $("input[type='checkbox']").change(function() {
               var smi  = $(this).val();
                   // alert(smi);
               $.ajax({
                url: 'listhotelregisration',
                type: "post",
                data: {'checko':smi},
                success: function(data){
                  //alert(data);
                }
              });  

   });

Route part:

Route::post('listhotelregisration', 'ListhotelController@create');

Controller part:

public function create(Request $request)
{
    $listhotel = new listhotel;
    $data = $request->all();
    $dataPrim = explode(",", $data);

    $listhotel->id_list= $dataPrim[0];
    $listhotel->id_hotel= $dataPrim[1]; 

    $listhotel->save();
    $response = "ok";
    return response ()->json ($response);

}
H.IHYA
  • 27
  • 1
  • 2
  • 10
  • A question on the AJAX part. I don't see any CSRF token sent along with the AJAX request. The 500 error may be because of the CSRF token not verified. – Yi Doe Dec 02 '16 at 11:53
  • Yeah, I just found that I need to add "csrf-token" header/input, thanks – H.IHYA Dec 02 '16 at 12:06

2 Answers2

1

Ajax in Laravel 5

This is mainly due to The VerifyCSRFToken middleware that laravel provide us out-of-the-box. It responds an Internal Server Error (500) when token mismatch occur on our requests.

We need to send our AJAX request with token sent to our browser. The CSRF token is mostly stored in the meta tag, you can put it any where though

$.ajaxSetup

Set default values for future Ajax requests

Solution

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

Hope this helps.

Yi Doe
  • 162
  • 1
  • 10
0

error 500 will mean this is a server error. To know exactly what went wrong you need to check the response the server is returning.

In any case i would already adjust your following pieces:

Javascript:

 $(function() {
          $("input[type='checkbox']").change(function() {
           var id_list  = $(this).attr('id_list');
           var id_hotel  = $(this).attr('id_hotel');  

           $.ajax({
            url: 'listhotelregisration',
            type: "post",
            data: {
                    'id_list': id_list,
                    'id_hotel':id_hotel
                  }
             }
       });  
  });

Controller:

public function create(Request $request)
{
  $data = $request->only(['id_list', 'id_hotel']);
  $listhotel = listhotel::firstOrCreate($data)
  return response ()->json ("OK");
}
Frederiek
  • 1,653
  • 1
  • 16
  • 27