开发者

How to specify DELETE method in a link or form?

开发者 https://www.devze.com 2023-03-25 03:36 出处:网络
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 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.

Is it possible to create a link or form in a html page that uses a request method t开发者_JAVA技巧hat is not GET or POST?


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:

<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.


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.

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


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

That said, with a little JavaScript, it's possible. Rails for instance ships with helpers which will add a data-method attribute to links. Rails-UJS is a jQuery library that will transparently intercept clicks on these links, and trigger a form submit with a _method parameter used for overriding the normal HTTP method. Finally Rack will intercept requests with a _method params, and overwrite the request method with the value in _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 Rack all work together to provide this.

It's good to know how your libraries work.


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.

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

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


@Ifnot plugin is great but I created a one based on $.ajax function instead of appending hidden forms! here's a simple example for a DELETE request

HTML

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

JavaScript

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

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


HTMX is designed to solve this.

Why should only <a> and <form> be able to make HTTP requests? Why should only click & submit events trigger them? Why should only GET & POST methods be available? Why should you only be able to replace the entire screen? By removing these arbitrary constraints, htmx completes HTML as a hypertext

It enables any DOM element to make HTTP requests GET, POST, PUT, PATCH, and DELETE, without writing any custom JavaScript.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号