AJAX上的VerifyCsrfToken.php(第68行)中的TokenMismatchException

时间:2017-06-27 11:01:33

标签: jquery ajax laravel

我在laravelcollective上,我想提交一个帖子请求 但是我得到了

  

VerifyCsrfToken.php中的TokenMismatchException(第68行)

这是我的代码:

 function storeUser(url) {  
    var name = $('input[name=name]').val() ;
    var email = $('.email').val() ; 
    var password =  $('.password').val() ; 
    var role_id = $('select[name=role_id]').val () ; 
    var barangay_id = $('select[name=barangay_id]').val () ; 
    var status = $('select[name=status]').val();
    console.log(barangay_id) ; 
    $.ajax({ 
        method : 'POST', 
        url : url , 
        data : 
        {  
            role_id     : role_id ,
            email       : email  ,
            name        : name , 
            password    : password , 
            barangay_id : barangay_id ,
            status      : status  , 
        }, 
        success : function (data)  { 
            swal({
                title: "Success",
                text: "User saved successfuly",
                type: "error",
                confirmButtonColor: "#66cc99"
            });
        } , 
        error:  function (xhr,reason,ex)  
        { 
            if (xhr.status == 500){ 
                swal({
                    title: "500",
                    text: "Internal Server",
                    type: "error",
                    confirmButtonColor: "#66cc99"
                });
            }
            if(xhr.status == 422){ 
                var data = xhr.responseJSON ;
                for(let i in data) {
                    console.log(i,data[i][0]) ;
                    swal({ 
                        title : '422', 
                        text : 'Fill all the fields properly' ,
                        type: 'error' ,
                        confirmButtonColor: "#66cc99"
                    })
                }

            }
        }
    })
 }

以下是我的表单片段:

<div class="form-group">
    <label for="input-text" class="col-sm-2 control-label">Full Name</label>
    <div class="col-sm-10">
        {!! Form::text('name', null, ['id'=>'input-text','class'=>'form-control full-name','placeholder'=>'e.g Juan De la Cruz']) !!}
    </div>
</div>

<div class="form-group">
    <label for="input-text" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
        {!! Form::email('email' , null, ['id'=>'input-text','class'=>'form-control email','placeholder'=>'e.g testemail@email.com']) !!}
        <span class="availability"></span>
    </div>
</div>


<div class="form-group">
    <label for="input-text" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
        {!! Form::text('password',null,['class'=>'form-control password']) !!}
    </div>
</div>

<div class="form-group">
    <label for="input-text" class="col-sm-2 control-label">Role </label>
    <div class="col-sm-10">
        <select class="form-control" name="role_id"> 
            @foreach ($roles as $role ) 
            <option value="{{ $role->id }}">{{ $role->name }}</option>
            @endforeach
        </select>
    </div>
</div>


<div class="form-group">
    <label for="input-text" class="col-sm-2 control-label">Barangay </label>
    <div class="col-sm-10">
        <select class="form-control" name="barangay_id"> 
            @foreach($barangays as $barangay ) 
            <option value="{{ $barangay->id }}">{{ $barangay->name }}</option>
            @endforeach
        </select>
    </div>
</div>

<div class="form-group">
    <label for="input-text" class="col-sm-2 control-label">Status :  </label>
    <div class="col-sm-10">
        <select class="form-control" name="status"> 
            <option value="1">Activate</option>
            <option value="0">Pending</option>
        </select>
    </div>
</div>




{!! Form::submit($submitButtonText, ['class' => 'btn btn-success pull-right submitBtn']) !!}

这是我的控制器

public function store(StoreUserRequest $request) {
    $user = User::create(request(['role_id','email','name','password','barangay_id','status'] )) ; 
    return response()->json($user) ;

}

使用ajax时,为什么会出现内部服务器错误500。我的代码中有错误吗?

1 个答案:

答案 0 :(得分:3)

事先在你的JS中执行此操作:

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

在你看来(在头脑中):

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

参考:Documentation