我想使用主 Blade 上的按钮自动更新表格,并包含一些条目。
Controller :
public function update($id, Request $request)
{
$x='47'; $y='16';
$device_new = Device_new::where('deviceId', $id)->first();
$device_new->x = $x;
$device_new->y = $y;
$device_new->save();
return redirect('/welcome');
}
按钮:
<div>
<button type="submit" id="updatedevice">UPDATE MAP</button>
</div>
Javascript(JQuery):
$(document).ready(function() {
$('#updatedevice').click(function() {
updatedev();
});
function updatedev() {
$.ajax({
url:'/device_new',
method: 'POST',
});
}
现在我想,我应该在“updatedev”中定义一些内容来执行此更新。 我用 GET 方法做到了,但这有点令人困惑。
有什么想法吗,这可行吗,或者我应该尝试其他方式?
最佳答案
如果启用,post、put 路由需要 csrf token 。要传递此信息,请使用 _method
作为方法,使用 _token
作为 csrf token 。
将 method
更改为 _method
并添加 crsf token 。
function updatedev() {
$.ajax({
url:'/device_new',
_method: 'POST',
_token : "yourCsrfTokern!!"
});
}
还要检查您的路线并将 Route::get
更改为 Route::post
关于javascript - 使用 JavaScript 调用 Laravel Controller 中的 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59388000/