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

问题描述 投票:0回答:1

我一直在开发一个具有React.js前端和Python Flask后端(REST API)的应用程序,我想要做的一件事是通过POST请求将一个表格从React发送JSONized数据到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个回答
0
投票

您的网址似乎有问题

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

我认为这不是有效的网址格式

也许您需要使用作为网址:https://cors-anywhere.herokuapp.com/memes要么http://localhost:5000/memes

© www.soinside.com 2019 - 2024. All rights reserved.