【问题标题】:JavaScript POST fetch sends as OPTIONSJavaScript POST fetch 作为 OPTIONS 发送
【发布时间】:2021-12-27 18:45:02
【问题描述】:

我正在学习 JavaScript,我正在制作一个帐户登录页面作为学习项目。服务器正在使用 Python 的 Flask。 fetch 功能似乎没有像我想要的那样工作。一方面,它发送的是选项请求而不是 POST,即使我指定了 POST。另一件事是服务器没有收到数据,它显示为空白。代码如下:

var content = JSON.stringify({'username': username, 'password': password}) //username and password are user inputs, I had them print and these work fine.
var response = fetch(url+"/api/login", {
      method: "POST",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: content
    })
.then(response => {
  console.log("Success") //this doesn't print.
})

【问题讨论】:

标签: javascript api http post fetch


【解决方案1】:

这是一个preflight 请求

通过传回正确的 CORS 标头来处理选项请求。

然后还要处理 post 请求,由于 API 和网站的新 CORS 标准,这两个都是必需的。

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 2017-07-07
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多