这是我的ajax代码:

$('select[id=currency-data]').change(function () {

    var currency_id =   $(this).val();

    $.ajax({
        url: "{{Route('exchange-rate')}}",
        type: 'POST',
        data: {currency_id: currency_id },
        success: function(data){
            $('#ex-rate').val(data);
        }
    });
});


视图:

        {!! Form::label('currency_id', 'Currency:', ['class' => 'control-label']) !!}
        {!! Form::Select('currency_id', $currency_data, Input::old('currency_id'), ['id'=>'currency-data','class' => 'form-control','required']) !!}

        {!! Form::label('exchange_rate', 'Exchange Rate:', ['class' => 'control-label']) !!}
        {!! Form::input('number','exchange_rate', Input::old('exchange_rate'), ['id'=>'ex-rate','class' => 'form-control','readonly','required']) !!}


当我运行此命令时,我在VerifyCsrfToken.php中收到错误:TokenMismatchException。

有人可以提供解决方案吗???

最佳答案

将其添加到刀片文件的<head>标记上

<meta name="csrf-token" content="{{ csrf_token() }}">


在您的javascript上,紧接在jQuery库之后,调用:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});
</script>

10-08 16:55