9

I've got this resource in routes.php:

Route::resource('items', 'ItemsController', ['before' => 'admin_access']);

Trying to reach ItemsContoller@update method through AJAX but it's kicking out a 405 Method not allowed error:

var $inputs = $('input', row);

var id = $(row).find('.edit').data('id');

var data = $inputs.serializeJSON();

data['_token'] = $('input[name=_token]').val();
data['_method'] = 'PUT';

console.debug(data);

$.ajax({
    url: 'items/' + id,
    method: 'PUT',
    dataType: 'json',
    data: data,
    complete: function (data) {
        if (data.success) {
            itemsTable.ajax.reload();
        }
    }
});

Both the id and data variables contain the correct information.

This works fine when I do a standard form submission with PUT as the method (using anahkiasen/Former opener method).

What am I missing here?

eComEvo
  • 11,669
  • 26
  • 89
  • 145
  • Judging by the error message (which appears to be coming from the server) and the little I've read about Laravel, this appears to be a case of your PUT request URL and your route handler are not in alignment and thus Laravel does not find a handler for that combination and thus returns 405. – jfriend00 Dec 17 '15 at 22:12

1 Answers1

18

Most browsers can't send PUT methods and are restricted to just GET and POST.

Try changing the method to POST, but leave your _method element in the data array to spoof the request method.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Ugghh, I completely forgot about this limitation. That was it. Thanks! – eComEvo Jul 25 '15 at 21:19
  • Or so I thought. When I changed `method: 'PUT'` to `method: 'POST'` it worked, but wasn't getting the JSON response expected, so I added `contentType: 'application/json'` and now I'm back to the 405 error. Any ideas? – eComEvo Jul 25 '15 at 21:46
  • 1
    Fixed this by appending `'?_method=PUT'` to the end of the `url` value. Wasn't being detected by Laravel after I changed the `contentType` to json. – eComEvo Jul 25 '15 at 22:17
  • 1
    Why do you way that "most browsers can't send PUT methods"? I just tried a PUT request in the current versions of Chrome, Firefox and in IE 11 and they all worked just fine to my own server. Both jQuery and MDN clearly document that PUT requests are supported. – jfriend00 Dec 16 '15 at 22:05
  • @jfriend00 Sorry. Poorly worded on my behalf; also from the MDN docs (https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_Requests_Methods_in_HTML_Forms): “only GET and POST method are allowed by the HTML specification.” Some browsers may be able to send PUT requests these days, but it’s not valid in the HTML spec. Seems fine for AJAX but again, not every browser may support PUT. Chrome, Firefox, and IE may, but what about older browsers, or browsers on mobile and tablet devices, e-readers etc? – Martin Bean Dec 17 '15 at 13:21
  • The HTML specification in this regard applies to form submissions, not to Ajax calls. Have you yet identified ANY browser in current use that doesn't support PUT via an Ajax call? – jfriend00 Dec 17 '15 at 14:32
  • @jfriend00 I said it was poorly worded, admitted it was the HTML specification that only allows GET and POST requests, and that PUT (and other methods) seemed fine to use in AJAX requests. No, I haven’t identified a browser that _doesn’t_ support PUT requests, but then again I haven’t tested. I imagine not 100% of browsers support PUT requests, so to make the app as usable as possible not to rely on it doing so. You can dismount your high horse now. – Martin Bean Dec 17 '15 at 16:26