PugJS 在表单操作中添加问号

huangapple go评论63阅读模式
英文:

PugJS adding question mark in form action

问题

以下是您要翻译的部分:

"form(method="DELETE", action='/shortener/'+ item._id)
button.btn.btn-delete(type="submit") Delete"

"The URL I get:

http://localhost:8080/shortener/6421f534e7c2fbc3293e83ad?
```"

"I also tried to get the url using fetch()

```public async destroy(req: Request, res: Response): Promise<Response> {
  await Shortener.findOneAndDelete({ _id: req.params.id });
  return res.status(204).json([]);
}```"

<details>
<summary>英文:</summary>

PugJS adding question mark in form action

form(method="DELETE", action='/shortener/'+ item._id)
button.btn.btn-delete(type="submit") Delete

The URL I get:

http://localhost:8080/shortener/6421f534e7c2fbc3293e83ad?


I also tried to get the url using fetch()

public async destroy(req: Request, res: Response): Promise<Response> {
await Shortener.findOneAndDelete({ _id: req.params.id });
return res.status(204).json([]);
}


</details>


# 答案1
**得分**: 1

"`DELETE`" 不是`method`属性的有效值,因此浏览器会忽略它并恢复默认值(即`GET`)。

当您提交一个`GET`表单时,会添加查询字符串到`action`中(或者替换掉已有的),该查询字符串由一个`?`后跟表单数据的键值对组成(在这种情况下没有表单数据,所以只会得到`?`)。

------

如果您想发起`DELETE`请求,那么您需要使用JavaScript而不是常规的表单提交。

如果您想通过表单提交来告诉服务器删除某些内容,那么请使用`POST`而不是`DELETE`(作为副作用,`POST`不会添加查询字符串)。

[1]: 参考链接:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#method

<details>
<summary>英文:</summary>

`DELETE` is not a valid value for [the `method` attribute][1], so the browser ignores it and reverts to the default (which is `GET`).

When you submit a GET form a query string on the `action` is added (or replaced if there is one there already) which consists of a `?` followed by the key=value pairs of the form data (in this case there is no form data so you just get the `?`).

------

If you want to make a DELETE request then you need to use JavaScript instead of a regular form submission.

If you want to make a form submission to tell the server to delete something, then use `POST`, not `DELETE` (as a side effect, POST won&#39;t add the query string).


  [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#method

</details>



huangapple
  • 本文由 发表于 2023年4月10日 18:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定