简体   繁体   中英

Get Internal Server Error 500 using this code on laravel 5.1

I always get internal server error, i was searching for solution but i can't get the best solution to solve my problem. How do I fix this issue? Internal Server Error 500. Her is my code

Router:

Route::get('getdata', function()
{
$term = Str::lower(Input::get('term'));
$data = array(
    'R' => 'Red',
    'O' => 'Orange',
    'Y' => 'Yellow',
    'G' => 'Green',
    'B' => 'Blue',
    'I' => 'Indigo',
    'V' => 'Violet',
);
$return_array = array();

foreach ($data as $k => $v) {
    if (strpos(Str::lower($v), $term) !== FALSE) {
        $return_array[] = array('value' => $v, 'id' =>$k);
    }
}
return Response::json($return_array);
});

my blade:

    <?= Form::open() ?>
    <?= Form::label('auto', 'Find a color: ') ?>
    <?= Form::text('auto', '', array('id' => 'auto'))?>
    <br>
    <?= Form::label('response', 'Our color key: ') ?>
    <?= Form::text('response', '', array('id' =>'response', 'disabled' => 'disabled')) ?>
    <?= Form::close() ?>

    <script type="text/javascript">
        $(function() {
            $("#auto").autocomplete({
                source: "getdata",
                minLength: 1,
                select: function( event, ui ) {
                    $('#response').val(ui.item.id);
                }
            });
        });
    </script>

Laravel 5.1 uses something called csrf protection for protecting site from Cross Site Request Forgery(CSRF).For Any type of form value POST to server, Laravel puts a hidden input field containing a valid token like this. This token is generated by laravel for each applications. Like This:

<input name="_token" type="hidden" value="oEUWsddRCEER123btOyuiZdATJANP83uERSz">

Any request without this token will be considered Forgery and laravel will deny the request and give them a Internal Server Error.

So, to Make a valid ajax request you need to specify the valid token of your app. Example (taken form laravel.io):

<meta name="csrf_token" content="{{ csrf_token() }}

Then during ajax call, customize the ajax call a bit to include the token data: I haven't tested this code. But you will get some idea:

<script type="text/javascript">
    $(function() {
        $("#auto").autocomplete({
            source: function (request, response) {
                 $.ajax({
                  type: "POST",
                  beforeSend: function (xhr) {
                  var token = $('meta[name="csrf_token"]').attr('content');
                  if (token) {
                      return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                     }
                  },
                  url:"getdata",

                  data: {},
                  success: response,

                }),

            select: function( event, ui ) {
                $('#response').val(ui.item.id);
            }
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM