1

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)
Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    Does `$device_mac` have any spaces or illegal URL characters in it? – jfriend00 Dec 16 '15 at 19:18
  • No, there shouldn't be a space in it. Why do I have a space somewhere ? – code-8 Dec 16 '15 at 19:20
  • Check exactly the URL you are getting inside the `$.ajax`. – azeós Dec 16 '15 at 19:21
  • 1
    Then, I'd suggest you do a `console.log()` of the URL and/or look in the network tab of the debugger and see exactly what request URL is being sent over the wire. – jfriend00 Dec 16 '15 at 19:21
  • `Request URL:http://localhost:8888/000D6766F2F6/device/080027E2FC7D` Keep cutting off my last 2 segments. – code-8 Dec 16 '15 at 19:23
  • It should be Request `URL:http://localhost:8888/000D6766F2F6/device/080027E2FC7D/rate/update` – code-8 Dec 16 '15 at 19:24
  • Try using Laravel to build out the URL. `url: {{ route('device.rate.update', [env("APP_URL"), $cpe_mac, $device_mac] }},` – user1669496 Dec 16 '15 at 19:25
  • @user3158900 : This doesn't work either. Your code missing a closing `)` – code-8 Dec 16 '15 at 19:29
  • If you are generating the js code within blade, show us the generated HTML with the jQuery function. – azeós Dec 16 '15 at 19:30
  • I'd suggest you do a View/Source in the browser to see what this ajax code source looks like in the actual generated Javascript. Then, you can figure out whether your problem is before or after that and narrow down the search. – jfriend00 Dec 16 '15 at 19:31
  • @azeós : I've added my HTML form that generated by **blade**. – code-8 Dec 16 '15 at 19:34
  • But you are missing the jQuery part, that's what we need to see. Or do what @jfriend00 suggest. – azeós Dec 16 '15 at 19:35
  • @jfriend00 : Is it because I PUT a `defected` JSON ? – code-8 Dec 16 '15 at 19:35
  • @azeós : jQuery Part added – code-8 Dec 16 '15 at 19:36
  • That jQuery is what you have inside the blade file. What we need to see is the result of that, the HTML output. Do a view source as @jfriend00 said, copy the jQuery part and paste it here. – azeós Dec 16 '15 at 19:38
  • are you using laravel 5 or laravel 5.1? Unless you're using both versions, you shouldn't include both tags. – Stan Shaw Dec 16 '15 at 19:40
  • @Stan Additional `laravel-5.1` added to specify version, you can see in tag info _5.1 version of Laravel framework. Use it in addition to the laravel-5 tag if your question is specific to Laravel 5.1._. – Zakaria Acharki Dec 16 '15 at 19:41
  • @ZakariaAcharki there are three tags: "laravel", "laravel 5" and "laravel 5.1". I would think that either "laravel" and "laravel 5" OR "laravel" and "laravel 5.1" should be included, unless he is using both versions 5 and 5.1? Isn't that like tagging ".Net", ".Net 4.0" and ".Net 4.5"? I think I'm misunderstanding how to properly use tags, here? – Stan Shaw Dec 16 '15 at 19:44
  • I think what @user3158900 suggested is almost right. `url: '{{ route('device.rate.update', [$cpe_mac, $device_mac]) }}'`. Try that. – azeós Dec 16 '15 at 19:50
  • @azeós : Still get `PUT http://localhost:8888/000D6766F2F6/device/080027E2FC7D 405 (Method Not Allowed)` – code-8 Dec 16 '15 at 19:52
  • Then please show us the generated HTML. – azeós Dec 16 '15 at 19:55
  • If you change your `url: ...` section to `url: '{{ url("/".$cpe_mac."/device/".$device_mac."/rate/update") }}'` does it affect anything? I have never tried using `env("APP_URL")`, it looks odd to me. – Tim Lewis Dec 16 '15 at 21:11

3 Answers3

2

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

Source:

Vikas
  • 993
  • 1
  • 10
  • 16
  • Good answer, I suspect this is the problem. – ceejayoz Dec 16 '15 at 20:28
  • I agree with you. Update my route to POST, and update my Ajax `type: 'POST',`, and still the same result. – code-8 Dec 16 '15 at 20:28
  • Did I miss anything else beside that 2 thing ? – code-8 Dec 16 '15 at 20:28
  • Can you check in the network logs if '_method' is being passed in the request ? – Vikas Dec 16 '15 at 20:31
  • I got `_method: "PUT"` in my Request Payload Section. – code-8 Dec 16 '15 at 20:34
  • Actually if you just change the ajax to POST and leave everything as it is, should still be a PUT request because of the method spoofing. What you are missing is the HTML code :p – azeós Dec 16 '15 at 20:36
  • Your Route should still be PUT not POST, just change AJAX's type( or method ) POST. – Vikas Dec 16 '15 at 20:37
  • Where do you get that most browsers don't support PUT requests on Ajax calls? – jfriend00 Dec 16 '15 at 20:40
  • @jfriend00 check [this](https://baxeico.wordpress.com/2014/06/25/put-and-delete-http-requests-with-django-and-jquery/) , it's not much authentic but after reading it and the error "405 (Method Not Allowed)" faced by OP, it makes sense. – Vikas Dec 16 '15 at 20:48
  • I just tried a "PUT" ajax request in Chrome, Firefox and IE 11 to my own server and it worked just fine. There is no browser restriction on using PUT requests via Ajax. It works. It's also documented on [MDN here](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest). So, the first sentence of your answer is just wrong. I have no idea if the OP has some sort of server issue with a PUT request or not. – jfriend00 Dec 16 '15 at 21:59
1

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);
    }
});
user1669496
  • 32,176
  • 9
  • 73
  • 65
1

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-

Venkat.R
  • 7,420
  • 5
  • 42
  • 63