当我使用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
})
}
Tags:
嗨,我也有同样的问题。我用了URLSearchParams
这是我的密码