Is there any way to force an <a href>
to do a delete request when clicked instead of a regular GET
?
有什么方法可以强制在单击时执行删除请求,而不是常规的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.
“Rails”的方法是动态地生成一个
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?
我不确定上面的AJAX功能是否有效,因为它只是理论上的。有什么办法吗?
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.
注意:请不要建议我在link_to上使用rails:method =>:delete,因为我在很多方面都试图避免使用rails helper,因为它们会用晦涩的javascript污染您的DOM。我希望这个解决方案放在一个外部JS文件中,并在需要时包含在其中。
3 个解决方案
#1
2
According to the docs:
根据文档:
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/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?
在大多数web浏览器中,PUT、DELETE、HEAD等方法都可用吗?
#2
1
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.
但是我不知道哪些浏览器支持它,哪些不支持。我在mac上用Firefox 2和3.6、safari 3和chrome 5.0.375.70测试了它。
#3
1
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
您可以使用RESTInTag插件,它将为您完成ajax请求,您只需向HTML标记添加一些额外的属性
Example:
例子:
HTML
HTML
<button class="delete" data-target="my/path/to/delete" data-method="DELETE" data-disabled="true">Delete Article</button>
JavaScript
JavaScript
$(".delete").restintag(optionsObj, function() {
do_refresh = true
},
function() {
alert("some error happened");
});
#1
2
According to the docs:
根据文档:
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/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?
在大多数web浏览器中,PUT、DELETE、HEAD等方法都可用吗?
#2
1
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.
但是我不知道哪些浏览器支持它,哪些不支持。我在mac上用Firefox 2和3.6、safari 3和chrome 5.0.375.70测试了它。
#3
1
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
您可以使用RESTInTag插件,它将为您完成ajax请求,您只需向HTML标记添加一些额外的属性
Example:
例子:
HTML
HTML
<button class="delete" data-target="my/path/to/delete" data-method="DELETE" data-disabled="true">Delete Article</button>
JavaScript
JavaScript
$(".delete").restintag(optionsObj, function() {
do_refresh = true
},
function() {
alert("some error happened");
});