【发布时间】:2021-02-14 23:24:01
【问题描述】:
我在使用 Fastapi、Jinja2 put 方法时遇到问题。 我的任务是保存表单,这里是 html 文件中的 ajax
function submitForm() {
var url = 'http://localhost:8000';
let id = document.getElementById("person-id").value;
var data = {id: id};
fetch(url, {
method: 'PUT',
body: JSON.stringify(data),
headers:{
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.log('Error:', error));
这里py方法更新
@router.put("/{id}", response_class=Response)
async def update(request: Request, id: str, req: UpdateModel = Body(...)):
updated = await update(id, req.dict())
return templates.TemplateResponse('index.html', context={'request': request})
更新不起作用,控制台只是显示
不允许使用 405 方法 T
【问题讨论】:
-
您的
url变量只是指向http://localhost:8000。它需要指向http://localhost:8000/{id} -
@im_baby 只允许 405 方法,除此之外没有错误。我的意思是 uvicorn 在其他部分起作用
-
@im_baby 是的,因为这是放的,当我检查Referer时:我得到了这个值localhost:8000/id
-
在 Postman 中尝试一下,看看结果如何。当你 PUT 到
http://localhost:8000时,你得到 405,当你 PUT 到http://localhost:8000/{id}时,你得到 200