[英]Getting CSRF token mismatch Laravel
I'm trying to submit a form with javascript but I keep getting this error CSRF token mismatch
.我正在尝试使用 javascript 提交表单,但我不断收到此错误
CSRF token mismatch
。 I tried to change var url = "{{ route('review.store') }}";
我试图改变
var url = "{{ route('review.store') }}";
to {{ route('review.store') }}?_token={{ csrf_token() }}
and it was submitting the form to the database without data.到
{{ route('review.store') }}?_token={{ csrf_token() }}
并且它在没有数据的情况下将表单提交到数据库。 I have seen a lot of question similar to this but I didn't get a solution for my case.How can I fix this?我已经看到很多类似的问题,但我没有得到解决我的案例的问题。我该如何解决这个问题?
<form id="form" enctype="multipart/form-data">
<input type="hidden" value="{{csrf_token()}}" id="token"/>
<div class="form-group" >
<label for="title">Title</label>
<input type="text" name="title" >
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" name="description">
</div>
</form>
<input type='button' value="Submit" id="btn"/>
Javascript Javascript
var token = $("#token").val();
$(document).ready(function(){
$("#btn").click(function(event){
event.preventDefault();
var url = "{{ route('review.store') }}";
var form = $('#form')[0];
var formData = new FormData(form);
formData.append('_token', token);
$.ajax({
url: url,
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
success:function(data){
if($.isEmptyObject(data.error)){
$("#msg").html("successfull");
$("#msg").fadeOut(3000);
}
}
});
});
});
Route路线
Route::resource('review','ProductReviewController');
You are missing the name _token in hidden field , Just serialize the form and send request
您在隐藏字段中缺少名称 _token ,只需序列化表单并发送请求
<form id="form" enctype="multipart/form-data">
<input type="hidden" name ="_token" value="{{csrf_token()}}" id="token"/>
<div class="form-group" >
<label for="title">Title</label>
<input type="text" name="title" >
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" name="description">
</div>
<input type='button' value="Submit" id="btn"/>
</form>
$(document).ready(function(){
$("#btn").click(function(event){
event.preventDefault();
var url = "{{ route('review.store') }}";
var formData = $('#form').serializeArray();
$.ajax({
url: url,
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
success:function(data){
if($.isEmptyObject(data.error)){
$("#msg").html("successfull");
$("#msg").fadeOut(3000);
}
}
});
});
});
In your jquery, please get the token like this:在您的 jquery 中,请像这样获取令牌:
var data = {
"_token": $('#token').val()
};
You can also try like你也可以试试像
data: {
"_token": "{{ csrf_token() }}",
"id": myid
}
So you ajax request would be所以你的ajax请求将是
$.ajax({
url : "url",
method:"post",
data : {
"_token": $('#csrf-token')[0].content
},
// Other stuffs to do
});
Try to use like this please and than serialize your form data请尝试这样使用,而不是序列化您的表单数据
<form id="form" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group" >
<label for="title">Title</label>
<input type="text" name="title" >
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" name="description">
</div>
<input type='button' value="Submit" id="btn"/>
</form>
尝试使用@csrf
或{{ csrf_field() }}
you can still try this.你仍然可以试试这个。 i takeaway the form tag and the csrf token on the form
我带走了表单上的表单标签和 csrf 令牌
//form //形式
<div class="form-group" >
<label for="title">Title</label>
<input type="text" id="title" name="title" >
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" id="description" name="description">
</div>
</form>
<input type='button' class="btn-submit" value="Submit" id="btn"/>
//ajax call //ajax调用
<script>
$(function() {
$(".btn-submit").click(function(e){
var title= $("input[name=title]").val();
var description= $("input[name=description]").val();
$.ajax({
url: murl +'/your-url',
type: "post",
data: {'title':title, 'description':description, '_token': $('input[name=_token]').val()},
success: function(data){
console.log(data);
}
});
});
});
</script>
The best way to solve this problem "X-CSRF-TOKEN" is to add the following code to your main layout, and continue making your ajax calls normally:解决这个问题“X-CSRF-TOKEN”的最好方法是在你的主布局中添加以下代码,并继续正常进行ajax调用:
In header在标题中
<meta name="csrf-token" content="{{ csrf_token() }}" />
In Form , add anywhere csrf token inside the form在 Form 中,在表单内的任意位置添加 csrf 标记
<form>
{{ csrf_field() }}
</form>
In script在脚本中
<script type="text/javascript">
$.ajax({
url: url,
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
success:function(data){
if($.isEmptyObject(data.error)){
$("#msg").html("successfull");
$("#msg").fadeOut(3000);
}
}
});
</script>
I hope it will works for you我希望它对你有用
I suggest that you should use我建议你应该使用
@csrf
And和
@method("PUT")
inside your form HTML tag, then new FormData()
handles the crsf and the method inputs在表单 HTML 标签中,然后
new FormData()
处理 crsf 和方法输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.