Html 如何在链接或表单中指定 DELETE 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6926512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:52:12  来源:igfitidea点击:

How to specify DELETE method in a link or form?

htmlhyperlinkhttpverbshttp-deleterfc2616

提问by Maurício C Antunes

Rfc2616 lists many methods besides GET and POST, like, say, DELETE, PUT etc. Method field in html forms, though, seems to be allowed to specify only GET or POST.

Rfc2616 列出了除 GET 和 POST 之外的许多方法,例如 DELETE、PUT 等。不过,html 表单中的方法字段似乎只允许指定 GET 或 POST。

Is it possible to create a link or form in a html page that uses a request method that is not GET or POST?

是否可以在使用非 GET 或 POST 请求方法的 html 页面中创建链接或表单?

回答by Paul D. Waite

You certainly can't create a link that uses anything other than GET. Since HTML began, links have been meant to be idempotent and free from side effects.

您当然不能创建使用GET. 从 HTML 开始,链接就被认为是幂等的并且没有副作用。

For forms and XMLHTTPRequests, Caps' link is the place to look: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?.

对于表单和XMLHTTPRequests,Caps 的链接是查看的地方:在大多数网络浏览器中是否可以使用 PUT、DELETE、HEAD 等方法?.

回答by AlmightyWhy

Was trying to figure this out for a rails app that was using Angular on the front end; these seems to work for that environment:

试图为在前端使用 Angular 的 Rails 应用程序解决这个问题;这些似乎适用于该环境:

<a data-confirm="Are you sure?" data-method="delete" href="/link-to-resource" rel="nofollow">Delete</a>

Edit: Just to give everyone a heads up, I think you still need to have jQuery for this to work. I removed jQuery and it stopped working; I put it back and it started working.

编辑:只是给大家提个醒,我认为您仍然需要使用 jQuery 才能使其正常工作。我删除了 jQuery,它停止工作了;我把它放回去,它开始工作了。

回答by Adam Sanderson

By default, not there is no way to do this. Links always perform GETs, forms can use GETs or POSTs.

默认情况下,不是没有办法做到这一点。链接总是执行 GET,表单可以使用 GET 或 POST

That said, with a little JavaScript, it's possible. Rails for instance ships with helpers which will add a data-methodattribute to links. Rails-UJS is a jQuery library that will transparently intercept clicks on these links, and trigger a form submit with a _methodparameter used for overriding the normal HTTP method. Finally Rack will intercept requests with a _methodparams, and overwrite the request method with the value in _method.

也就是说,使用一点 JavaScript,这是可能的。例如,Rails 附带了帮助器,它会data-method为链接添加一个属性。Rails-UJS 是一个 jQuery 库,它会透明地拦截对这些链接的点击,并_method使用用于覆盖普通 HTTP 方法的参数触发表单提交。最后 Rack 会拦截带有_methodparams 的请求,并用 中的值覆盖请求方法_method

Other frameworks no doubt follow a similar pattern.

其他框架无疑遵循类似的模式。

If you want even more details, I've written up an explanation of how Rails, Rails-UJS, and Rackall work together to provide this.

如果你想了解更多细节,我已经写了一篇关于Rails、Rails-UJS 和 Rack如何协同工作来提供这些的解释。

It's good to know how your libraries work.

很高兴知道您的库是如何工作的。

回答by Ifnot

It is not possible to create a link or form with delete method.

无法使用删除方法创建链接或表单。

Many web framework create a hidden input called "_method" for handling PUT and DELETE.

许多 Web 框架创建了一个名为“_method”的隐藏输入,用于处理 PUT 和 DELETE。

I created a plugin for automatically convert links to forms : RestfulizerJs

我创建了一个插件来自动将链接转换为表单:RestfulizerJs

You can take a look here : https://github.com/Ifnot/RestfulizerJs

你可以看看这里:https: //github.com/Ifnot/RestfulizerJs

回答by Khaled Al-Ansari

@Ifnot plugin is great but I created a one based on $.ajaxfunction instead of appending hiddenforms! here's a simple example for a DELETErequest

@Ifnot 插件很棒,但我创建了一个基于$.ajax功能而不是附加hidden表单的插件!这是一个简单的DELETE请求示例

HTML

HTML

<button class="delete" data-target="http://example.com/post/post-id/" data-method="DELETE" data-disabled="true">Delete Article</button>

JavaScript

JavaScript

$(".delete").restintag(optionsObj, function(data) {
    console.log(data);
},
function(error) {
    console.log(error);
});

https://github.com/KhaledElAnsari/RESTInTag/

https://github.com/KhaledElAnsari/RESTInTag/

回答by Edward Verenich

just add a data-method attribute,<a data-method='put' href='whatever'>link</a>

只需添加一个数据方法属性,<a data-method='put' href='whatever'>link</a>