【问题标题】:Getting 404 when trying to make POST request from React frontend to Flask backend尝试从 React 前端向 Flask 后端发出 POST 请求时出现 404
【发布时间】:2020-05-24 11:31:11
【问题描述】:

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

但是,我不断收到 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

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

提前致谢!

【问题讨论】:

    标签: json reactjs flask post http-status-code-404


    【解决方案1】:

    您的网址似乎有问题

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

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

    也许你需要使用 url: https://cors-anywhere.herokuapp.com/memes 或者 http://localhost:5000/memes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      相关资源
      最近更新 更多