这里我展示我的代码。我已经在视图文件的头部包含了csrf meta。如果可以解决此问题,或者需要更改,请提供意见。
这是路线。
Route::post('/addcontact','ContactController@addcontact');
这里是视图文件。
<div class="modal-body" >
<form align = "left" id = "form-insert" method = "post" action = "{{url('/addcontact')}}">
{{csrf_field()}}
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Religion</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<input id="signup-token" name="_token" type="hidden" value="{{csrf_token()}}">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
这是脚本...
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#form-insert").on('submit',function(e){
e.preventDefault();
var data = $(this).serialize();
var url = $(this).attr('action');
var post = $(this).attr('method');
$.ajax({
type : post,
url : url,
data : data,
success:function(data){
alert('success')
}
});
});
</script>
这是控制器...
public function addcontact(Request $request){
$contacts = Contact::create($request->all());
}