PugJS 在表单动作中添加问号
form(method="DELETE", action='/shortener/'+ item._id)
button.btn.btn-delete(type="submit") Delete
我得到的网址:
http://localhost:8080/shortener/6421f534e7c2fbc3293e83ad?
我还尝试使用 fetch() 获取 url
public async destroy(req: Request, res: Response): Promise<Response> {
await Shortener.findOneAndDelete({ _id: req.params.id });
return res.status(204).json([]);
}
DELETE
不是 method
属性 的有效值,因此浏览器将忽略它并恢复为默认值(即 GET
)。
当您提交 GET 表单时,
action
上的查询字符串将被添加(或替换,如果已经存在的话),其中包含一个 ?
后跟表单数据的键=值对(在这种情况下有没有表格数据,所以你只得到?
)。
如果你想发出删除请求,那么你需要使用 JavaScript 而不是常规的表单提交。
如果你想提交一个表单来告诉服务器删除一些东西,那么使用
POST
,而不是 DELETE
(作为副作用,POST 不会添加查询字符串)。
这来自 DELETE 方法。 DELETE 向 URL 附加一个问号。将方法更改为 POST。
form(method="POST", action='/shortener/'+ item._id)
button.btn.btn-delete(type="submit") Delete
或者直接剪掉问号
var url = 'http://localhost:8080/shortener/6421f534e7c2fbc3293e83ad?';
url = url.replace(/\?$/, '');