我得到令牌不匹配異常,每件事看起來都是正確的。laravel 5.2中的TokenMismatchException ajax調用
這是我的視圖代碼
@extends('layouts.master')
@section('content')
<div class="bg"></div>
<div class="login-sec clearfix">
<h1 class="lg-logo">YOU ARE JUST ONE STEP AHEAD</h1>
<div class="login-box clear">
<form id="websiteform" action="#">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-md-12 form-field">
<label for="">Project Name</label>
<input type="text" name="project" id="project">
</div>
<div class="col-md-12 form-field">
<label for="">Website URL</label>
<input type="url" name="website_url" id="website_url">
</div>
<div class="col-md-12 form-field">
<button type="submit" class="btn-green btn-large">Add Your Website</button>
</div>
</form>
</div>
</div>
@stop
和形式源頭部和頁腳是
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="csrf-token" content="4ryCSznz0mPSBcwXvbmZZHkZGcxZpyDy2dQ1VAoc" />
<link href="http://localhost:8080/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="http://localhost:8080/js/jquery.min.js"></script>
<script src="http://localhost:8080/js/bootstrap.min.js"></script>
<script src="http://localhost:8080/js/main.js"></script>
和main.js我的AJAX調用
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#websiteform').submit(function(e){
e.preventDefault();
var pro=$('#project').val();
var url=$('#website_url').val();
$.ajax({
type: "POST",
url: "project_save",
data: {project: pro, url: url},
success: function(data)
{
if(data.success==false)
{
alert(data.error);
}
else{
window.location = "dashboard";
}
},
error:function(){}
});
});
});
和路由和控制器功能
Route::post('/project_save','[email protected]_save');
public function project_save()
{
if(Request::ajax()){
$validator = Validator::make(Request::all(), [
'project' => 'required|max:255',
'url' => 'required',
]);
if($validator->fails()){
return Response::json(['success'=>false,'error'=>$validator->errors()->toArray()]);
}
$Website = new Website;
$Website->project_name = Request::get('project');
$Website->website_url = Request::get('url');
$Website->user_id = Auth::id();
$Website->save();
return Response::json(['success'=>true]);
}
}
這是我的代碼和我收到tokenmismatach exeption。
刪除輸入隱藏字段csrf_token和檢查 – sanu
那場是沒有必要的,但它不會打破AJAX調用 –
我刪除,但同樣的問題.. –