3

I have a jquery script(downloaded from github) that deletes the entities. Following is the script.

$(document).ready(function() {


var restful = {

    init: function(elem) {
        elem.on('click', function(e) {
            self=$(this);
            e.preventDefault();

            if(confirm('Are you sure you want to delete this record ? Note : The record will be deleted permanently from the database!')) {
                $.ajax({
                    headers: {
                        Accept : "text/plain; charset=utf-8",
                        "Content-Type": "text/plain; charset=utf-8"
                    },
                    url: self.attr('href'),
                    method: 'DELETE',
                    success: function(data) {
                        self.closest('li').remove();
                    },
                    error: function(data) {
                        alert("Error while deleting.");
                        console.log(data);
                    }
                });
            }
        })
    }
};

restful.init($('.rest-delete'));

});

and i use it as such

{{link_to_route('download.delete','x', ['id' => $download->id], array('class'=> 'rest-delete label label-danger')) }}

The corresponding laravel route is as follows

Route::delete('/deletedownload/{id}', array('uses' => 'DownloadsController@deletedownload', 'as'=>'download.delete'));

However I am getting a 405 Method not allowed error when I try to press the X (delete button). The error is as follows

DELETE http://production:1234/deletedownload/42 405 (Method Not Allowed) . 

This is working fine on my local sandbox.

Any help will be well appreciated.

thanks

Gagan
  • 5,416
  • 13
  • 58
  • 86
  • Not sure if this is a typo but your route is `/deletedownload/` but you are calling `/deletevideo/` – nullability May 27 '14 at 20:36
  • that was a typo.. I have corrected it .. basically there are two routes one for the downloads and one for the videos. They are both using this same script. I am facing the same problem while accessing both of them. – Gagan May 27 '14 at 20:39

1 Answers1

2

You have used method:DELETE instead use following in your ajax call

$.ajax({
    headers: {...},
    url: self.attr('href'),
    type:"post",
    data: { _method:"DELETE" },
    success: function(data) {...},
    error: function(data) {...}
});

Laravel will look for the _method in POST and then the DELETE request will be used if found the method.

Update: (Because of this answer, pointed by nietonfir)

You may try DELETE method directly like this (if it doesn't work then try the other one), :

$.ajax({
    headers: {...},
    url: self.attr('href'),
    type:"DELETE",
    success: function(data) {...},
    error: function(data) {...}
});
Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • The method option is ok, only misspelled: it's called "type". ;-) – nietonfir May 27 '14 at 21:54
  • All browsers doesn't work with `DELETE` method, it's a way to send the required method to `post`, `Laravel` then internally makes `DELETE` request. – The Alpha May 27 '14 at 21:59
  • It totally is, just [search around](http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers). Afaik IE8 has (had) problems with PUT/DELETE in AJAX calls, but except for that dinosaur it just works. – nietonfir May 27 '14 at 22:31
  • @WereWolf-TheAlpha .. i tried it both the methods but I am still getting the same message .. Failed to load resource: the server responded with a status of 405 (Method Not Allowed) – Gagan May 28 '14 at 01:21
  • @If i removed the headers, tried with your code again (both of them ) I am still getting the message "Delete ... 405 method not allowed " – Gagan May 28 '14 at 01:26
  • @WereWolf-TheAlpha .. sorry there was a problem with the code. I verified that and your first code snippet works fine for me. – Gagan May 28 '14 at 02:52