gpt4 book ai didi

javascript - 从 AJAX laravel 获取 http ://localhost/scl-mgt-update/fetch? type=teacher 500(内部服务器错误)

转载 作者:行者123 更新时间:2023-12-01 08:29:39 27 4
gpt4 key购买 nike

Ajax 无法正常工作。有人可以帮我吗?

在 Blade 中

<div class="col-xl-8 col-lg-8 col-12 form-group" id="div-type">
<label>Expense Type</label>
<select class="select2" name="type" id="selectType">
<option></option>
<option value="Teacher's Salary" {{ old('type')=="Teacher's Salary" ? 'selected' : '' }}>
Teacher's Salary
</option>
<option value="Staff's Salary" {{ old('type')=="Staff's Salary" ? 'selected' : '' }}>Staff's
Salary
</option>
</select>
</div>
<div id="div-name" class="hidden"></div>

jquery Ajax 代码

$('#selectType').on('change', function () {
if ($(this).val() == "Teacher's Salary") {
$('#content').hide();
$.ajax({
url: "{{ route('fetch-data') }}",
method: 'GET',
data: {type: 'teacher'},
dataType: 'json',
success: function (data) {
alert(data);
$('#div-type').attr('class', 'col-xl-4 col-lg-4 col-12 form-group');
$('#div-name').attr('class', 'col-xl-4 col-lg-4 col-12 form-group');

$('#div-name').html(data);

$('#teacher_name').select2();
},
});
} else if ($(this).val() == "Staff's Salary") {
$('#content').hide();
} else {
$('#content').show();
}
});

在 web.php

Route::get('/fetch', 'ExpenseController@ajaxFetch')->name('fetch-data')

在 Controller 中

public function ajaxFetch(Request $request)
{
if ($request->ajax()) {
$type = $request->get('type');
if ($type == 'teacher') {
$names = Teacher::all();
$data = '
<label>Select Teacher</label>
<select id="teacher_name" class="select2">
';
foreach ($names as $item) {
$data+='<option value="'+$item->id+'">'+$item->name+'</option>';
}
$data+='</select>';
}
return json_encode($data);
}
}

现在它不起作用了。在控制台中 GET http://localhost/scl-mgt-update/fetch?type=teacher 500(内部服务器错误)来自 AJAX laravel 此错误显示。我现在可以做什么来运行ajax?相同的代码正在用于搜索。当我在 Controller 中生成数据时,它会向我发送错误。否则 ajax 警报会起作用。

最佳答案

这样做,+在jquery中使用在php中使用

public function ajaxFetch(Request $request)
{
if ($request->ajax()) {
$type = $request->get('type');
if ($type == 'teacher') {
$names = Teacher::all();
$data = '
<label>Select Teacher</label>
<select id="teacher_name" class="select2">
';
foreach ($names as $item) {
$data .= '<option value="'.$item->id.'">'.$item->name.'</option>';
}
$data .='</select>';
}
return json_encode($data);
}
}

关于javascript - 从 AJAX laravel 获取 http ://localhost/scl-mgt-update/fetch? type=teacher 500(内部服务器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61898953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com