admin管理员组文章数量:1406182
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_refresh; //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 remend 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.
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_refresh; //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 remend 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.
- 2 Rails 3 does this with unobtrusive javascript. I don't know all the ins and outs myself, but here's a start: metaskills/2010/1/29/… – Jarrod Commented Jun 15, 2010 at 15:15
3 Answers
Reset to default 2According to the docs:
http://api.jquery./jQuery.ajax/
the DELETE method is allowed but not supported on all browsers, so you should take a look here:
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
This is what i do:
$("a.delete").live('click', function() {
$.post(this.href, "_method=delete", function(data) {
//do refresh
});
return false;
})
But i don't really know which all browsers support it and which don't. I tested it on mac with Firefox 2 and 3.6, safari 3 and chrome beta 5.0.375.70 and it works.
You can use RESTInTag plugin, it will do the ajax requests for you all you have to do is add a couple of extra attributes to your HTML tag
Example:
HTML
<button class="delete" data-target="my/path/to/delete" data-method="DELETE" data-disabled="true">Delete Article</button>
JavaScript
$(".delete").restintag(optionsObj, function() {
do_refresh = true
},
function() {
alert("some error happened");
});
本文标签: javascriptDoing a DELETE request from ltagt tag without constructing formsStack Overflow
版权声明:本文标题:javascript - Doing a DELETE request from <a> tag without constructing forms? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744968439a2635091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论