更新数据库,PUT 405 方法不允许错误
我正在尝试通过 PUT 方法更新我的数据库:
const eventid = arg.event.id;
const eventData = {
start: arg.event.start.toISOString(),
end: arg.event.end.toISOString(),
};
const csrfToken = document.head.querySelector("[name~=csrf-token][content]").content;
console.log(csrfToken);
fetch(`/api/event/update/${eventid}`, {
method: 'PUT',
headers: {
"X-CSRF-Token": csrfToken
},
body: encodeFormData(eventData),
})
具体错误是“PUT HTTP://localhost:8000/api/event/update/105 405(不允许方法)”。 我尝试过其他方法,但它们都返回类似的错误。另外,这是我的控制器代码:
public function update(Request $request)
{
$booking = Booking::findOrFail($request->id);
$booking->start_date = $request->start;
$booking->end_date = $request->end;
$booking->save();
return response()->json($booking);
}
路线:
Route::post('/event/update/{eventid}', [CalendarController::class, 'update']);
这是在我的路线中使用“post”的问题吗?
I am trying to update my database via a PUT method:
const eventid = arg.event.id;
const eventData = {
start: arg.event.start.toISOString(),
end: arg.event.end.toISOString(),
};
const csrfToken = document.head.querySelector("[name~=csrf-token][content]").content;
console.log(csrfToken);
fetch(`/api/event/update/${eventid}`, {
method: 'PUT',
headers: {
"X-CSRF-Token": csrfToken
},
body: encodeFormData(eventData),
})
The specific error is `PUT HTTP://localhost:8000/api/event/update/105 405 (Method not allowed).
I have tried other methods but they all return similar errors. Additionally here is my Controller code:
public function update(Request $request)
{
$booking = Booking::findOrFail($request->id);
$booking->start_date = $request->start;
$booking->end_date = $request->end;
$booking->save();
return response()->json($booking);
}
Route:
Route::post('/event/update/{eventid}', [CalendarController::class, 'update']);
Is this an issue with using 'post' in my route?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
了解变量是否返回您期望的结果的最佳方法是在编写代码时进行测试。
根据我的经验,我发现当我在声明和实例化变量的每个点上使用 dd() 变量时,尤其是那些传递给函数的变量,它让我省去了很多麻烦。
更改您的更新方法需要从: 更改
为:
The best way to know that your variables return what you expect them to return is to test as you code.
From my experience, I find that when I dd() variables at every point I declare and instantiate a variable, especially those passed to a function, it saves me a lot of headache.
Change your update method needs to change from:
To: