尝试从 React 前端向 Flask 后端发出 POST 请求时获取 404

Getting 404 when trying to make POST request from React frontend to Flask backend

我一直在开发一个带有 React.js 前端和 Python Flask 后端 (REST API) 的应用程序,我想要的一件事是发送表单通过 POST 请求从 React 到我的 Flask 后端的 JSONified 数据,以便它可以被处理。

但是,我一直收到 404(未找到)HTTP 错误,这真的很令人沮丧,因为我真的不知道如何调试它。我不确定在请求之间发生了什么以及为什么会这样。

这是相关的 React 前端代码:

handleSubmit = (e) => {
        e.preventDefault();
        const text = {
            text : this.state.content
        };
        const sendText = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    },
            body: JSON.stringify(text)
        };
        console.log(sendText.body);
        const url = 'https://cors-anywhere.herokuapp.com/http://localhost:5000/memes';
        fetch(url, sendText).then(response => response.json())
            .then(data => console.log(data));
        console.log("Success!") 

这是相关的 Flask 代码

@app.route('/memes', methods=['POST'])
def memes():
    data = request.get_json()
    text = data["text"]
    return jsonify({"result": "success!", "text": text}), 200

知道出了什么问题吗?请随时询问有关我在做什么或正在使用什么的任何问题。

提前致谢!

您的 url

似乎有问题

const url = 'https://cors-anywhere.herokuapp.com/http://localhost:5000/memes';

我不认为这是有效的 url 格式

也许您需要使用 as url: https://cors-anywhere.herokuapp.com/memes 要么 http://localhost:5000/memes