[英]Python FastApi error 422 (Unprocessable Entity)
當我使用 POST 請求將表單數據從運行在端口 5500 上的 JS 腳本使用 XMLHttpRequest 發送到我的后端(在端口 8000 上運行的 FastAPI 應用程序)時,我總是遇到此錯誤Unprocessable Entity
。 這是服務器端:
origins = ["https://localhost:5500"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/test")
def test(username: str = Form(...)):
print(f"########### Received {username} #############")
這是客戶端代碼:
async function sendData() {
formData = new FormData();
formData.append('username', 'Kareem');
const response = await fetch('http://localhost:8000/test', {
method: 'POST',
data: formData
})
}
嗨,我有同樣的問題。 我使用了 URLSearchParams。
這是我的代碼
let urlencoded = new URLSearchParams(); urlencoded.append("username", username); urlencoded.append("password", password); try { setLoading(true); const response = await fetch("http://127.0.0.1:5000/login", { method: 'POST', headers: { "Content-Type" : "application/x-www-form-urlencoded" }, body: urlencoded, redirect: 'follow' })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.