我制作了一个斗牛士的配置器,用户可以在其中插入其宽度、高度、长度、颜色和其他详细信息等。我想在用户在 url 中输入其 id 时执行此操作,以便获取他或她存储在数据库中的所有记录。
public function view($id)
{
$formData = FormData::findOrFail($id);
if (!$formData) {
abort(404);
};
return view('polebar', ['formData'=> $formData]);
}
这是控制器文件
Route::post('/quote/{id}', [FormController::class, 'view'])->name('quote.view');
这是路线
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '{{ route("/quote/") }}',
method: 'POST',
data: formData,
success: function(response) {
console.log("response", response);
if (response.status) {
alert('Data saved successfully!');
window.location.href = '/quote/' + response.quote_id;
}
},
error: function(xhr) {
alert('An error occurred. Please try again.');
}
});
这是ajax
我希望从数据库中获取数据,它只是在前端更改其值,结果以 json 形式显示给我
// controller file
public function view($id)
{
$formData = FormData::findOrFail($id);
return view('polebar', ['formData'=> $formData]);
}
// Ajax code
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var Id = 1; // mention accurate id
$.ajax({
url: '/quote/' + Id,
method: 'GET',
data: {}, // You can pass additional data if needed
success: function(response) {
console.log("response", response);
if (response.status) {
alert('Data fetched successfully!');
window.location.href = '/quote/' + response.quote_id;
} else {
alert('Failed to fetch data.');
}
},
error: function(xhr) {
alert('An error occurred. Please try again.');
}
});
// Route file
Route::get('/quote/{id}', [FormController::class, 'view'])->name('quote.view');
note: modify ajax code.