第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

POST 方法在被請(qǐng)求時(shí)不斷發(fā)送 400 錯(cuò)誤

POST 方法在被請(qǐng)求時(shí)不斷發(fā)送 400 錯(cuò)誤

開心每一天1111 2023-05-16 14:21:57
我目前一直在使用 Flask 和 MongoDB 來試驗(yàn)一個(gè)小型應(yīng)用程序。我目前正在讓用戶獲取 MongoDB 集合中的所有法則,并通過將 JSON 發(fā)送到服務(wù)器,通過 POST 方法為 MongoDB 集合創(chuàng)建新法則。但是,下面的輸出顯示向 /post/law 發(fā)送 POST 請(qǐng)求將發(fā)出 400 錯(cuò)誤。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 -我對(duì)為什么會(huì)發(fā)生此錯(cuò)誤感到有些困惑。我知道 400 錯(cuò)誤通常與錯(cuò)誤請(qǐng)求有關(guān),但是,我不確定錯(cuò)誤請(qǐng)求錯(cuò)誤發(fā)生在哪里?# 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>
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

可能是因?yàn)槟囊晥D沒有返回響應(yīng)。嘗試:


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

def post_law():

    if request.is_json:

        content = request.get_json()

        print(content)

    return "hello"

另外,您的網(wǎng)址格式不正確。應(yīng)該:


 await fetch("http://127.0.0.1:8000/post/law", options)

而且,為什么所有的async電話?唯一應(yīng)該異步的是你的fetch()


您也有 2 個(gè)submits發(fā)生。嘗試這個(gè):


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

<h1>Submit a Law</h1>

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

    <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="button" value="Submit law" onclick="submitLaw();">

</form>


<script>

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

    function submitLaw() {

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

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

        let url = 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.0.1:8000/post/law", options)


        if (response.ok) {

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

        } else {

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

        }

    }

</script>


查看完整回答
反對(duì) 回復(fù) 2023-05-16
  • 1 回答
  • 0 關(guān)注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)