Laravel 5.0中的TokenMismatchException

时间:2016-06-08 14:23:41

标签: ajax laravel

我在form内有div

<div class="form-container col-sm-12 container-fluid">
<meta content="{{ csrf_token() }}" name="_token">
<form class="form" id="register_form" method="POST" novalidate>
    <!-- <input type="hidden" value="{{ csrf_token() }}" name="_token" id="token"> -->
    <div class="form-group" id="error_msg"></div>
    <div class="form-group name">                        
        <input type="text" class="form-control input-sm" role="group" id="name" placeholder="Name" name="name" required>                  
    </div>
    <div class="form-group email"> 
        <input type="email" class="form-control input-sm" id="email" placeholder="Email" name="email" required>      
    </div>
    <div class="form-group contact_no">
        <input type="text" class="form-control input-sm" id="contact_no" placeholder="Contact No" name="contact_no" required>
    </div>
    <div class="form-group select_level">
        <select class="form-control input-sm" name="level_interested" id="level_interested" required>
            <option class="form-control" selected disabled value="">Level Interested</option>
            <option class="form-control" value="bachelor">Bachelor</option>
            <option class="form-control" value="masters">Masters</option>
            <option class="form-control" value="phd">Phd</option>
            <option class="form-control" value="nursing">Nursing</option>
        </select>
    </div>
    <div class="form-group select_country">
        <select class="form-control input-sm" name="country_interested" id="country_interested" required>
            <option class="form-control" selected disabled value="">Country Interested</option>
            <option class="form-control" value="usa">U.S.A</option>
            <option class="form-control" value="australia">Australia</option>
            <option class="form-control" value="canada">Canada</option>
            <option class="form-control" value="india">India</option>
            <option class="form-control" value="new_zealand">New Zealand</option>
        </select>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-success btn-sm">Register</button>
    </div>
</form>    

提交form后,将继续进行验证。验证成功后,我使用Ajax发送输入数据并显示警告消息作为响应。

form.submit(function() {
        if(validateName() & validateEmail() & validateContact_No() & validateLevel() & validateCountry()) {

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

            $.ajax({
                type : 'post',

            url: "{{ route('save_registry') }}",

            data: {
                'name' : name.val(),
                'email' : email.val(),
                'contact_no' : contact_no.val(),
                'level_interested' : level_interested.val(),
                'country_interested' : country_interested.val(),
            },

            success : function(data) {

                    alert(data);

            }
        });
            return true;
        } else {
            $('#error_msg').text("Please Enter Details Correctly").show().addClass('text-danger').addClass('text-center');
            return false;
        }

这是我function中的Controller来处理请求。

public function save_registry(Request $request)
{


    $new_registry = new Register();

    $email = Register::where('email','=',$request->email)->exists();
    if($email==true)
    {
        return 'Email already exists';
    }
    else{
        $new_registry -> name = $request -> name;
        $new_registry -> email = $request -> email;
        $new_registry -> contact_no = $request -> contact_no;
        $new_registry -> level_interested = $request -> level_interested;
        $new_registry -> country_interested = $request -> country_interested;
        $new_registry->save();
        return 'Registered Successfully';
    }
}

提交form后,会弹出提醒。关闭警报后,我收到TokenMismatchException错误。仅当form's method POST不在GET时才会出现此错误。那么有什么解决方案可以在methodPOST时无法获得错误吗?

1 个答案:

答案 0 :(得分:1)

当您通过_token提交表单时,Laravel会查找名为POST的属性。我发现您已经在<meta>标记中显示了此内容,因此当您POST表单时,有两种方法可以实现此目的。

首先,您可以在<input type="hidden" name="_token" value="{{ csrf_token() }}"/>的某个位置创建新的<form>元素,并确保在方法为POST时通过您的ajax传递:

data: {
    '_token' : _token.val(),
    'name' : name.val(),
    'email' : email.val(),
    'contact_no' : contact_no.val(),
    'level_interested' : level_interested.val(),
    'country_interested' : country_interested.val(),
},

或者,由于您已经在<meta>标记中添加了该标记,因此您可以将其包含在您的ajax中,如下所示:

data: {
    '_token' : $('meta[name="_token"]').attr('content'),
    'name' : name.val(),
    'email' : email.val(),
    'contact_no' : contact_no.val(),
    'level_interested' : level_interested.val(),
    'country_interested' : country_interested.val(),
},

无论您如何操作,只需确保在_token数据中包含POST属性,它就适合您。希望有所帮助!