表单操作中的相对 URL 路径不起作用

Relative URL path in form action not working

我正在制作一个表单,但点击该表单的提交按钮并没有带我到正确的路径。

<form action="delete" method="post">
    <button type="button">Cancel</button>
    <button type="submit">Delete</button>
</form>

它应该带我去 http://localhost:5000/<id>/delete (代替将是用户 ID)

但它需要我 http://localhost:5000/delete

http://localhost:5000/<id>/delete是网页不是文件。这意味着这个网站不是静态的,而是动态的。

我做错了什么?

Note: Assuming your delete is a file and it is in the same directory (id/) as your current html file.

关于路径的小事:

  • pathname 是绝对的。这意味着它将在根目录中搜索路径名。 (在本例中是您的 webapp 的根文件夹)
  • ./pathname 是相对的。 . 表示当前目录(也就是您的文件(例如 index.html)所在的目录)。
  • ../pathname也是相对的。 .. 表示您所在的当前文件夹之上的一个文件夹。

示例:

rootFolder/
  test.html
  anotherFolder/
    otherFile.html
  folder/
    index.html
    delete.html

现在假设您在 folder/

  • test.html -> 有效
  • ./test.html -> 不存在,因为 . == rootFolder/folder/
  • ../anotherFolder/otherFile.html -> 有效,因为 .. 向上移动一个文件夹(从 folder/rootFolder)然后输入 anotherFolder/otherFile.html

当您打开终端并cd(更改目录)时,同样的原则也适用。您可以先尝试一下该路径是否有效。

要回答你的问题,应该是:action="./delete"你现在使用的是绝对路径。

您应该尝试从 URL 中提取 ID,然后将该 ID 设置到您的表单操作中,例如:

按照这个 Whosebug 问题从 url How can I get query string values in JavaScript?

中提取 ID
<form action="/<id>/delete" method="post">
    <button type="button">Cancel</button>
    <button type="submit">Delete</button>
</form>