当我单击“提交”时,我会收到此消息。
ReferenceError:未定义输入
当我创建新数据并单击保存或提交按钮时,如何在console.log中获取最后一个ID号?
控制器
public function store(Request $request)
{
if($request->ajax()) {
$school = new SchoolsList();
$school->user_id = auth()->user()->id;
$school->province_id = $request->province_id;
$school->city_id = $request->city_id;
$school->center_id = $request->center_id;
$school->save();
return response()->json(['data_school' => $request->all(), 'id' => $school->id]);
}
}
Ajax
<script type="text/javascript">
$("#schools").submit(function (event) {
event.preventDefault();
data_school = input.value;
province_id = $('#province_id').val();
city_id = $('#city_id').val();
center_id = $('#center_id').val();
$.post("{{ route('schools-list.store') }}", {province_id:province_id, city_id:city_id, center_id:center_id}, function (data_school) {
console.log(response.data_school.id), function (response) {
console.log(response.data_school.id);
}
});
});
</script>
答案 0 :(得分:0)
在您的JavaScript中,您有“ data_school = input.value;”
input
未定义,也不需要。
只需删除它即可。
代码console.log(response.data_school.id)
的最后一行也是错误的。您设置自己的数据结构,即response.id