无法在 Laravel 资源控制器中获取 post Ajax 数据
Cannot get post Ajax data in Laravel Resource Controller
我想在不刷新页面的情况下提交表单。
下面是我的 jquery Ajax 代码:(代码引用了这个网站 https://code.tutsplus.com/tutorials/submit-a-form-without-page-refresh-using-jquery--net-59)
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#Calibrationform').submit(function(event) {
var dataString = $(this).serialize();
$.ajax({
url: "/Equipments",
data: dataString,
type: 'POST',
processData: false,
success: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
},
error: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Failed to Submit!</h2>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
});
</script>
<div id="response_message"></div>
<form id="Calibrationform" class="static p-3 col-span-8 col-start-3" enctype="multipart/form-data">
@csrf
<input type="hidden" name="request_type" value="Update Calibration for All Category">
<div class="w-full rounded-lg shadow-lg p-4">
<div class="flex flex-wrap -mx-3 mb-6 col-start-3 col-span-8 row-start-2">
<div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
Location
</label>
<select id="Calibration_Filter_Location" name="Calibration_Location"
class="form-select appearance-none block mb-3 w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option style="display: none"></option>
@foreach ($locations as $location)
<option value="{{$location['Location']}}">{{
$location['Location'] }}</option>
@endforeach
</select>
<span style="color: red">@error('Calibration_Location'){{ $message
}}@enderror</span>
</div>
<div class="w-full md:w-1/4 px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-last-name">
Equipment's Category
</label>
<select id="Calibration_Filter_Category" name="Calibration_Category"
class="form-select appearance-none block mb-3 w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option style="display: none"></option>
@foreach ($categories as $category)
<option value="{{$category['Category']}}">{{
$category['Category'] }}</option>
@endforeach
</select>
<span style="color: red">@error('Calibration_Category'){{ $message
}}@enderror</span>
</div>
<div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
Calibration Date
</label>
<input
class="appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white"
id="grid-first-name" type="date" name="Date_of_Calibration">
<span style="color: red">@error('Date_of_Calibration'){{ $message }}@enderror</span>
</div>
<div class="w-full md:w-1/4 px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2">
Next Due Date
</label>
<input
class="appearance-none block mb-3 w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
id="grid-last-name" type="date" name="Next_Due_Date">
<span style="color: red">@error('Next_Due_Date'){{ $message }}@enderror</span>
</div>
</div>
<div class="w-full overflow-hidden rounded-lg shadow-xs">
<div class="w-full overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Equipment Name
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Registration Tag
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Status
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Cal Cert
</th>
<th scope="col" class="relative px-6 py-3">
</th>
</tr>
</thead>
<tbody id="Calibration_Table" class="bg-white divide-y divide-gray-200">
</tbody>
</table>
</div>
</div>
</div>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</div>
问题是我检查了我的网络,状态为 200,但我的资源控制器存储方法不是 运行,因为我使用 dd(request()->all()) 进行检查,但功能不是也被处决了。
希望能得到您的一些提示,在此先感谢
添加后return response()->json(["status"=>"success"]);
您可以在 ajax
中提交表单数据
$('#Calibrationform').submit(function(event) {
var bodyFormData = new FormData(this);
$.ajax({
url: "/Equipments",
data: bodyFormData,
type: 'POST',
contentType: false,
processData: false,
success: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
},
error: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Failed to Submit!</h2>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
});
我想在不刷新页面的情况下提交表单。
下面是我的 jquery Ajax 代码:(代码引用了这个网站 https://code.tutsplus.com/tutorials/submit-a-form-without-page-refresh-using-jquery--net-59)
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#Calibrationform').submit(function(event) {
var dataString = $(this).serialize();
$.ajax({
url: "/Equipments",
data: dataString,
type: 'POST',
processData: false,
success: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
},
error: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Failed to Submit!</h2>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
});
</script>
<div id="response_message"></div>
<form id="Calibrationform" class="static p-3 col-span-8 col-start-3" enctype="multipart/form-data">
@csrf
<input type="hidden" name="request_type" value="Update Calibration for All Category">
<div class="w-full rounded-lg shadow-lg p-4">
<div class="flex flex-wrap -mx-3 mb-6 col-start-3 col-span-8 row-start-2">
<div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
Location
</label>
<select id="Calibration_Filter_Location" name="Calibration_Location"
class="form-select appearance-none block mb-3 w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option style="display: none"></option>
@foreach ($locations as $location)
<option value="{{$location['Location']}}">{{
$location['Location'] }}</option>
@endforeach
</select>
<span style="color: red">@error('Calibration_Location'){{ $message
}}@enderror</span>
</div>
<div class="w-full md:w-1/4 px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-last-name">
Equipment's Category
</label>
<select id="Calibration_Filter_Category" name="Calibration_Category"
class="form-select appearance-none block mb-3 w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500">
<option style="display: none"></option>
@foreach ($categories as $category)
<option value="{{$category['Category']}}">{{
$category['Category'] }}</option>
@endforeach
</select>
<span style="color: red">@error('Calibration_Category'){{ $message
}}@enderror</span>
</div>
<div class="w-full md:w-1/4 px-3 mb-6 md:mb-0">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
Calibration Date
</label>
<input
class="appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white"
id="grid-first-name" type="date" name="Date_of_Calibration">
<span style="color: red">@error('Date_of_Calibration'){{ $message }}@enderror</span>
</div>
<div class="w-full md:w-1/4 px-3">
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2">
Next Due Date
</label>
<input
class="appearance-none block mb-3 w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
id="grid-last-name" type="date" name="Next_Due_Date">
<span style="color: red">@error('Next_Due_Date'){{ $message }}@enderror</span>
</div>
</div>
<div class="w-full overflow-hidden rounded-lg shadow-xs">
<div class="w-full overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Equipment Name
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Registration Tag
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Status
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Cal Cert
</th>
<th scope="col" class="relative px-6 py-3">
</th>
</tr>
</thead>
<tbody id="Calibration_Table" class="bg-white divide-y divide-gray-200">
</tbody>
</table>
</div>
</div>
</div>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</div>
问题是我检查了我的网络,状态为 200,但我的资源控制器存储方法不是 运行,因为我使用 dd(request()->all()) 进行检查,但功能不是也被处决了。
希望能得到您的一些提示,在此先感谢
添加后return response()->json(["status"=>"success"]);
您可以在 ajax
中提交表单数据 $('#Calibrationform').submit(function(event) {
var bodyFormData = new FormData(this);
$.ajax({
url: "/Equipments",
data: bodyFormData,
type: 'POST',
contentType: false,
processData: false,
success: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
},
error: function(data) {
$('#response_message').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Failed to Submit!</h2>")
.hide()
.fadeIn(1500, function() {
// $('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
});