【问题标题】:Fetch error - Receiving 405 GET Method Not Allowed While Using POST获取错误 - 使用 POST 时不允许接收 405 GET 方法
【发布时间】:2021-06-23 18:32:42
【问题描述】:

我正在尝试部署一个应用程序,我必须在其中注册才能从后端获取包含我的数据(usernameemailpassword)的 JSON。我通过 fetch 将前端链接到后端(代码已附加)。我的 fetch 请求有一个 POST 方法,但是我得到了这个错误,而我没有使用任何 GET 方法,为什么会这样?

fetch('link/signup' /*will be changed with correct URL tomorrow*/,{
    method: 'POST',
    /* method: 'POST',
    body: formData, */
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      username: username.value,
      email: email.value,
      password: password.value,
    }),
  }).then((response) => {
    return response.json();
  })
  .catch((error) => {
      console.log('Request failed', error);
  }); 

GET https://link/signup/ 405(不允许的方法)(匿名)@ 注册.9bbdb929a0502d2a723f.js:68

【问题讨论】:

  • GET link/signup 405(方法不允许)(匿名)@registration.9bbdb929a0502d2a723f.js:68
  • 酷,所以:你的开发工具网络标签说正在发生什么? 405 表示服务器不支持您选择的任何“方法”值,这表明您正在调用的 URL 无法识别 POST
  • 这很奇怪,我该如何解决?
  • 首先更新您的帖子以显示您实际看到的内容 =) 使用开发工具信息更新您的帖子(不要将其添加为答案:如果它不是你的问题,不要把它作为答案发布)另外,protip:只是用黑框空白敏感信息。不要在上面画黄色/蓝色。

标签: javascript fetch http-status-code-405


【解决方案1】:

这很奇怪,但有些 API 不区分大小写。尝试写 'post' 而不是 'POST' 看看

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 2017-11-13
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2019-07-05
    • 2013-12-05
    • 2012-10-26
    • 1970-01-01
    相关资源
    最近更新 更多