【发布时间】:2020-06-28 01:09:48
【问题描述】:
我正在尝试使用 ajax 提交表单,但出现错误 The POST method is not supported for this route. Supported methods: GET, HEAD. 我在 Stackoverflow 和其他来源上看到了很多问题和答案,但没有得到解决方案,我该如何解决这个问题?
刀片文件
<form method="POST" 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>
<button type='submit' id="btn" >submit
</form>
Javascript
<script>
$(document).ready(function(){
$("#btn").click(function(event){
event.preventDefault();
var url = '{{ route('review.store') }}';
var form = $('form')[0];
var formData = new FormData(form);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.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);
}
}
});
});
});
</script>
路线
Route::post('review', 'ProductReviewController@store')->name('review.store');
【问题讨论】:
标签: javascript ajax laravel laravel-5