我正在尝试使用 AJAX 发送多个参数。我的 JavaScript 代码如下所示。
$('.overview_value').change(function() {
$('#overview').DataTable({
destroy: true,
processing: true,
serverSide: true,
pageLength: 100,
responsive: true,
ajax: baseUrl+'/owner/safety/overview_result/'+$("#time").val()+'/'+$("#property_id").val()+'/'+$("#unit_id").val()+'/'+$("#status").val(),
order: [1, 'desc'],
ordering: false,
autoWidth: false,
drawCallback: function () {
$(".dataTables_length select").addClass("form-select form-select-sm");
},
language: {
'paginate': {
'previous': '<span class="iconify" data-icon="icons8:angle-left"></span>',
'next': '<span class="iconify" data-icon="icons8:angle-right"></span>'
}
},
columns: [
{
"data": "property_id",
},
{
"data": "unit_id",
},
{
"data": "issue_date",
},
{
"data": "expiry_date",
},
{
"data": "certified_by",
},
{
"data": "safety",
},
{
"data": "reminder",
},
]
});
});
我的路线如下。
Route::get('overview_result/{time}/{property}/{unit}/{status}', [OverviewController::class, 'overview_result'])->name('overview_result');
我的控制器代码如下。
public function overview_result( $time, $property, $unit, $status )
{
if ($time == 'all') {
$difference = Carbon::now()->addYears(100);
}
if ($time == 'oneweek') {
$difference = Carbon::now()->endOfWeek()->addWeek();
}
if ($time == 'onemonth') {
$difference = Carbon::now()->addMonth()->endOfMonth();
}
if ($time == 'threemonth') {
$difference = Carbon::now()->addMonths(3)->endOfMonth();
}
if ($time == 'sixmonth') {
$difference = Carbon::now()->addMonths(6)->endOfMonth();
}
$data = Safety::with(['property', 'propertyUnit'])
->where('property_id', $property)
->where('unit_id', $unit)
->whereBetween('expiry_date', [Carbon::now(), $difference])
->orderBy('id', 'DESC')
->get();
return $data;
}
如何在
overview_result
函数内接收多个参数?
像这样使用它不需要添加带 URL 的参数。只需像下面的代码一样添加数据数组即可。
<script type="text/javascript">
$(document).ready(function() {
$('#table1').DataTable({
"ajax": {
"url": baseUrl+'/owner/safety/overview_result/',
"data": function(d) {
// Add your custom parameters here
d.time = $("#time").val();
d.property_id = $("#property_id").val();
d.unit_id = $("#unit_id").val();
d.status = $("#status").val();
}
},
"columns": [
{
"data": "property_id",
},
{
"data": "unit_id",
},
{
"data": "issue_date",
},
{
"data": "expiry_date",
},
{
"data": "certified_by",
},
{
"data": "safety",
},
{
"data": "reminder",
},
]
});
});
</script>
并且在控制器中,您可以像这样接收这些参数。
use Illuminate\Http\Request;
class PropertyController extends Controller
{
public function overview_result(Request $request){
$request->query('time');
$request->query('property_id');
$request->query('unit_id');
$request->query('status');
}