Flask API 和 Reactjs 的多重请求

Multiple Request with Flask API and Reactjs

下面是我做的请求。一切正常,除非我尝试显示此错误的 put /stats-batch。不知道显示的options是什么,今天才发现

访问时/stats-batch也只是打印hello

27.0.0.1 - - [25/May/2021 16:20:46] "OPTIONS /user HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:46] "POST /user HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "OPTIONS /product HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "GET /product HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "OPTIONS /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "GET /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:21] "OPTIONS /new-batch HTTP/1.1" 200 -
oks
127.0.0.1 - - [25/May/2021 16:21:21] "PUT /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:21] "GET /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:43] "OPTIONS /stats-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:44] "PUT /stats-batch HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\USER\Desktop\Allen\santeh\env\Lib\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
.......
.......
  File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type function is not JSON serializable

我像这样使用 axios 单击按钮调用此函数。 configauthorization header 就像所有其他请求一样

const startBatch = async () => {
  const status = 'start'

  await axios
    .put('http://127.0.0.1:5000/stats-batch', status, config)
    .then((res) => {
      console.log(res)
    })
    .catch((err) => {
      console.log(err)
    })
}

控制台显示 errorNo Access-Control-Allow-Origin header is present on the requested resource

我也安装了 flask_cors,另一个 axios 请求正在运行。我不知道是后端还是前端导致此错误的原因。

我看到一个问题; “/new-batch”端点抛出 500 个异常,这可能与“Object 类型函数不是 JSON 可序列化”有关。

根据描述/错误“没有 Access-Control-Allow-Origin header 出现在请求的资源 上”消息我的理解是

  1. PUT VERB 缺少 CORS 配置。 因为您写道其余的端点可以正常工作,所以只有在此特定“/new-batch”端点上的 PUT 失败。

选项:在 CORS 中,使用 OPTIONS 方法发送预检请求,以便服务器可以响应发送请求。

URL: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS