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

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

如何創(chuàng)建一個(gè) if 語(yǔ)句來(lái)查找獲取請(qǐng)求?

如何創(chuàng)建一個(gè) if 語(yǔ)句來(lái)查找獲取請(qǐng)求?

瀟湘沐 2022-01-01 20:33:45
我正在使用 Flask 和 Fetch()我有一個(gè)表單當(dāng)文本輸入“a”更改/輸入文本時(shí)。一個(gè)獲取請(qǐng)求被發(fā)送到“/validate-form”。當(dāng)表單末尾的提交按鈕被按下時(shí)。它也將表單提交到“/validate-form”。我可以創(chuàng)建什么 If else 語(yǔ)句,它將能夠區(qū)分是發(fā)送獲取請(qǐng)求還是提交表單到“/validate-form”我已經(jīng)嘗試了下面的方法,但是“FETCH”一直被打印到終端上。@app.route("/validate-form", methods = ["POST"])def register():    if request.method == "POST":        if "username" in request.form and "password" in request.form:           print("submitted")           return "Thanks"        else:            print("FETCH")            return "Thanks"
查看完整描述

2 回答

?
繁星coding

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

它對(duì)我有用,它正確打印“提交”。


我可以提出的解決方案是使用一些隱藏的輸入,只有在單擊提交按鈕時(shí)才會(huì)發(fā)送這些輸入。


<input type="hidden" name="isFormSubmitted" value="true" />

更完整的例子:


前端:


<h1>Hello Flask</h1>

<form id="myForm" action="/validate-form" method="POST">

    <label>Password</label>

    <input name="password" type="password" />

    <label>Username</label>

    <input name="username" type="text" />

    <input type="hidden" name="isFormSubmitted" value="true" />

    <label>Validate when I am changed</label>

    <input name="validate" type="text" id="validate"/>

    <input type="submit" value="Send" />

</form>



<script>

window.onload = function() {

  const myForm = document.getElementById("myForm");

  const validateInput = document.getElementById("validate");

  //input or change event handler

  validate.addEventListener("input", function(e) {

    const data = new FormData(myForm);

    data.set("isFormSubmitted", "false");

    fetch('/validate-form', {

      method: 'POST',

      body: data,

    }).then((response) => {

        console.log(response);

    });

  })

}

</script>

后端:


@app.route("/validate-form", methods = ["POST"])

def register():

    if request.method == "POST":

        print(request.form)

        if request.form["isFormSubmitted"] == "true":

           print("SUBMITTED")

           return "Thanks submitted"

        else:

            print("FETCH")

            return "Thanks fetch"


查看完整回答
反對(duì) 回復(fù) 2022-01-01
?
哈士奇WWW

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

如果我很清楚你想要什么并且為了區(qū)分它,你可以在通過“POST”發(fā)送數(shù)據(jù)之前在javascript中測(cè)試事件類型,如下所示:


   if(window.event.type === 'submit'){

        // You are sending data

    } else {

       // You are validating data

    }


查看完整回答
反對(duì) 回復(fù) 2022-01-01
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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