Fulfilling DELETE <a> tag without creating forms?

Is there a way to force <a href>a delete request to be executed when clicked instead of the usual GET?

The "Rails" way is to dynamically generate a tag <form>that adds the appropriate parameters so that it is redirected to the right place, but I do not really like this DOM manipulation for such a simple task.

I was thinking about going the route:

$("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 that working on AJAX will work, as it is just theoretically. Is there any way to do this?

:
:method => :delete link_to, , DOM javascript. , JS .

+3
3

, :

$("a.delete").live('click', function() {
        $.post(this.href, "_method=delete", function(data) {
            //do refresh
        });
        return false;
    })

, , . Mac Firefox 2 3.6, Safari 3 Chrome 4.0.0.375.70, .

+1

You can use the RESTInTag plugin , it will make all ajax requests for you, what you need to do is add a few additional 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");
});
+1
source

Source: https://habr.com/ru/post/1750139/


All Articles