邮递员请求 "POST" 但反应请求 "OPTIONS"

postman requests are "POST" but react request are "OPTIONS"

我尝试与前面的 reactjs 和后面的烧瓶进行通信,但我遇到了这个问题: 当我用邮递员调用我的函数时,我得到这个:

127.0.0.1 - - [25/Mar/2022 16:53:34] "POST /add HTTP/1.1" 200 -

当我用我的 React js 前端调用它时:

127.0.0.1 - - [25/Mar/2022 16:52:16] "OPTIONS /add HTTP/1.1" 200 -

我查看旧的堆栈溢出,但找不到答案。

有我的烧瓶功能;


@app.route("/add", methods=["POST"], strict_slashes=False)
def add_articles():
    print("NONNNNNNNNNNNN")
    return "AHHHHH"

这是我的 api 电话:

export default class APIService{
    // Insert an article
    static InsertArticle(body){
        return fetch(`http://localhost:5000/add`,{
            'method':'POST',
             headers : {
            'Content-Type':'application/json'
      },
      body:JSON.stringify(body)
    })
    .then(response => response.json())
    .catch(error => console.log(error))
    }

}```

thanks for yours answers, i am lost

当 React 应用程序向 api 发出请求时,它需要先发出选项请求以进行 CORS 检查,因为它们位于不同的域中。您可以在开发人员工具的网络选项卡中验证每个 ajax 请求之前都有一个 OPTIONS 请求。

例如,您的 React 应用程序可能在 localhost:3000 中,而后端在 localhost:5000 域中。然后每个 ajax 请求都需要先进行 CORS 检查,因为前端和后端在技术上属于不同的域。您在 Postman 中看不到选项请求,因为 Postman 直接调用后端,因此它不需要 CORS 检查的选项请求。