在我從 React 表單完成對 Flask 后端的 POST 請求后,有沒有辦法可以保留在 React 前端(localhost 3000)上,而不是切換到 Flask 后端(localhost 5000)。我不想在我的燒瓶后端重定向。
相關代碼:
export const DateForm = () => {
return (
<div>
<form method='post' action='/acceptDates'>
<label> Enter Dates:
</label>
<br></br>
<input type="text" id='startDate' name='startDate' />
<br></br>
<input type="text" id='endDate' name='endDate' />
<br></br>
<input type='submit' value='submit' />
</form>
</div>
)
}
@app.route('/acceptDates', methods=['GET', 'POST'])
def get_dates():
if request.method == 'POST':
sd = request.form.get('startDate')
return "received dates"
提交表單后,我切換到我的 Flask 本地主機,但我想繼續使用 React。
uj5u.com熱心網友回復:
試試這個!
export const DateForm = () => {
const handleSubmit = (event) => {
event.preventDefault();
console.log("handleSubmit Runs");
// Do post request or get request
// Code here
};
return (
<div>
<form onSubmit={handleSubmit}>
<label> Enter Dates:</label>
<br></br>
<input type="text" id="startDate" name="startDate" />
<br></br>
<input type="text" id="endDate" name="endDate" />
<br></br>
<input type="submit" value="submit" />
</form>
</div>
);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/457641.html
標籤:javascript 反应
