更新数据库,PUT 405 方法不允许错误

发布于 2025-01-11 13:48:26 字数 1091 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-01-18 13:48:26

了解变量是否返回您期望的结果的最佳方法是在编写代码时进行测试。

根据我的经验,我发现当我在声明和实例化变量的每个点上使用 dd() 变量时,尤其是那些传递给函数的变量,它让我省去了很多麻烦。

更改您的更新方法需要从: 更改

  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);
    }

为:

   public function update(Request $request, $event_id)
    {
        $booking = Booking::findOrFail($event_id);
        $booking->start_date = $request->start;
        $booking->end_date = $request->end;
        $booking->save();

        return response()->json($booking);
    }

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:

  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);
    }

To:

   public function update(Request $request, $event_id)
    {
        $booking = Booking::findOrFail($event_id);
        $booking->start_date = $request->start;
        $booking->end_date = $request->end;
        $booking->save();

        return response()->json($booking);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文