POST 方法在被请求时不断发送 400 错误

我目前一直在使用 Flask 和 MongoDB 来试验一个小型应用程序。我目前正在让用户获取 MongoDB 集合中的所有法则,并通过将 JSON 发送到服务器,通过 POST 方法为 MongoDB 集合创建新法则。


但是,下面的输出显示向 /post/law 发送 POST 请求将发出 400 错误。


127.0.0.1 - - [12/Aug/2020 09:21:50] "GET / HTTP/1.1" 200 -

127.0.0.1 - - [12/Aug/2020 09:21:50] "GET /laws HTTP/1.1" 200 -

127.0.0.1 - - [12/Aug/2020 09:21:54] "POST /post/law HTTP/1.1" 400 -

我对为什么会发生此错误感到有些困惑。我知道 400 错误通常与错误请求有关,但是,我不确定错误请求错误发生在哪里?


# This handles the POST request

@app.route('/post/law', methods = ["POST"])

def post_law():

    if request.is_json:

        content = request.get_json()

        print(content)

客户端:


<!--This tries to send a request to /post/law-->

<h1>Submit a Law</h1>

<form action="", method="GET">

    <div id="law_name_section">

        <label>Name of the law:</label>

        <input type="text" required id="law_name">

    </div>

    <div id="law_description_section">

        <label>Description of the law:</label>

        <input type="text" required id="law_description">

    </div>

    <div id="law_url_section">

        <label>URL to the law:</label>

        <input type="text" required id="law_url">

    </div>

    <input type="submit" value="Submit law" onclick="submitLaw()">

</form>


<script>

// submitLaw() tries to send a request to /post/law in a JSON request

    async function submitLaw() {

        let name = await document.getElementById('law_name').value 

        let description = await document.getElementById('law_description').value 

        let url = await document.getElementById('law_url').value


        let options = {

            method: "POST",

            headers: { "Content-Type": "application/json" },

            body: {

                name: name,

                description: description,

                url: url,

                status: "Bill"

            }

        }


        let response = await fetch("http://127.0.01:8000/post/law", options)


        if (response.ok) {

            alert("Successfully sent data to /post/law")

        } else {

            alert(`Couldn't send data to /post/law`)

        }

    }

</script>


开心每一天1111
浏览 157回答 1
1回答

侃侃尔雅

可能是因为您的视图没有返回响应。尝试:@app.route('/post/law', methods = ["POST"])def post_law():&nbsp; &nbsp; if request.is_json:&nbsp; &nbsp; &nbsp; &nbsp; content = request.get_json()&nbsp; &nbsp; &nbsp; &nbsp; print(content)&nbsp; &nbsp; return "hello"另外,您的网址格式不正确。应该:&nbsp;await fetch("http://127.0.0.1:8000/post/law", options)而且,为什么所有的async电话?唯一应该异步的是你的fetch()您也有 2 个submits发生。尝试这个:<!--This tries to send a request to /post/law--><h1>Submit a Law</h1><form action="", method="POST">&nbsp; &nbsp; <div id="law_name_section">&nbsp; &nbsp; &nbsp; &nbsp; <label>Name of the law:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" required id="law_name">&nbsp; &nbsp; </div>&nbsp; &nbsp; <div id="law_description_section">&nbsp; &nbsp; &nbsp; &nbsp; <label>Description of the law:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" required id="law_description">&nbsp; &nbsp; </div>&nbsp; &nbsp; <div id="law_url_section">&nbsp; &nbsp; &nbsp; &nbsp; <label>URL to the law:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" required id="law_url">&nbsp; &nbsp; </div>&nbsp; &nbsp; <input type="button" value="Submit law" onclick="submitLaw();"></form><script>// submitLaw() tries to send a request to /post/law in a JSON request&nbsp; &nbsp; function submitLaw() {&nbsp; &nbsp; &nbsp; &nbsp; let name = document.getElementById('law_name').value;&nbsp; &nbsp; &nbsp; &nbsp; let description = document.getElementById('law_description').value;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; let url = document.getElementById('law_url').value;&nbsp; &nbsp; &nbsp; &nbsp; let options = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: { "Content-Type": "application/json" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: description,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: url,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status: "Bill"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; let response = await fetch("http://127.0.0.1:8000/post/law", options)&nbsp; &nbsp; &nbsp; &nbsp; if (response.ok) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Successfully sent data to /post/law")&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(`Couldn't send data to /post/law`)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python