code-8
code-8

Reputation: 58810

Method Not Allowed while doing Ajax

Route

Route::put('url/update',['as'=>'test.update', 'uses'=>'TestController@update']);

Ajax

$.ajax({
    url: 'url/update',
    type: 'PUT',
    dataType: 'json',
    data: $inputs ,
    success: function (data, textStatus, xhr) {
        console.log(data);
    },
    error: function (xhr, textStatus, errorThrown) {
        console.log('PUT error.', xhr, textStatus, errorThrown);
    }
});

Result

PUT http://localhost:80/url/update 405 (Method Not Allowed)

Upvotes: 1

Views: 3909

Answers (3)

Vikas
Vikas

Reputation: 993

Change the method to 'POST' and add a hidden element '_method' with value set to 'PUT' in the form.

Source:

Upvotes: 2

Venkat.R
Venkat.R

Reputation: 7746

HTML forms only support GET and POST, but it does understand a real PUT/PATCH request.

Other Notes:
1. Use Postman first to check your API. 1. Make sure your protocol is http / https.
1. Your Controller method should return JSON Format.
1. Make sure your received your inputs.

Refer to this Answer for few more info:
http://laravel.io/forum/02-13-2014-i-can-not-get-inputs-from-a-putpatch-request-

Upvotes: 1

user1669496
user1669496

Reputation: 33138

Sorry, my comment was not correct because I was not looking close enough at the structure. I'm pretty sure it would work if modified though.

I just setup the following route:

Route::put('{cpe_mac}/device/{device_mac}/rate/update', [ 'as'=> 'device.rate.update', 'uses' => 'DeviceController@updateRate']);

I added the javascript to the view:

$.ajax({
    url: '{{ route('device.rate.update', [$cpe_mac, $device_mac], true) }}',
    type: 'PUT',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: {
        some: 'test'
    },
    success: function(data) {
        console.log(data);
    },
    error: function(xhr) {
        console.log(xhr);
    }
});

I'm passing true as the 3rd argument so it builds a URL with an absolute path. I think it's a bit cleaner than trying to prepent env("APP_URL").

The result on the page was:

$.ajax({
    url: 'http://myapp.local/000D6766F2F6/device/080027E2FC7D/rate/update',
    type: 'PUT',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: {
        some: 'test'
    },
    success: function(data) {
        console.log(data);
    },
    error: function(xhr) {
        console.log(xhr);
    }
});

Upvotes: 1

Related Questions