Doing a DELETE request from <a> tag without constructing forms?
Posted
by Mike Trpcic
on Stack Overflow
See other posts from Stack Overflow
or by Mike Trpcic
Published on 2010-06-15T15:08:18Z
Indexed on
2010/06/15
15:12 UTC
Read the original article
Hit count: 124
Is there any way to force an <a href>
to do a delete request when clicked instead of a regular GET
?
The "Rails" way is to dynamically generate a <form>
tag that adds the appropriate params so that it gets routed to the right place, however I'm not too fond of that much DOM manipulation for such a simple task.
I thought about going the route of something like:
$("a.delete").click(function(){
var do_refresh = false;
$.ajax({
type: "DELETE",
url: "my/path/to/delete",
success: function(){
do_refresh = true;
},
failure: function(){
alert("There is an error");
}
});
return do_return; //If this is false, no refresh would happen. If it
//is true, it will do a page refresh.
});
I'm not sure if the above AJAX stuff will work, as it's just theoretical. Is there any way to go about this?
Note:
Please don't recommend that I use the rails :method => :delete
on link_to
, as I'm trying to get away from using the rails helpers in many senses as they pollute your DOM with obtrusive javascript. I'd like this solution to be put in an external JS file and be included as needed.
© Stack Overflow or respective owner