1

I'm using same form to insert, search and update data. For insert I'm using Laravel's store method and to search and update, using Ajax.

Record inserting and searching of text fields works fine but when updating record it throws an error "The POST method is not supported for this route. Supported methods: GET, HEAD, PUT."

Added blade directive @method('PUT') in form still showing same error.

Route:

Route::get('search-data/{sid?}', 'DataController@dataSearch');
Route::put('update-data/{sid?}', 'DataController@dataUpdate');

Controller:

use App\exModel;

public function dataSearch($sid)
    {
        $search = exModel::find($sid);
        return Response::json($search);
    }
    public function dataUpdate(Request $request, $sid)
    {
        $var = exModel::find($sid);
        $var->name = $request->name;
        $var->save();
    return Response::json($var);
    }

Script:

//Search
  $(document).ready(function () {
  $('#search').on('keydown', function(e) {
    if(e.which == 13){
    var sid = $("#search").val();
      $.ajax({
                  url: '{{ URL::to('search-data/')}}'+"/"+ sid,
                  type: "Get",
                  dataType: 'json',
                   success: function(response){ 
                    $('#Getname').val(response.name);
                 }
                });
            }
    });
     });
//Update
$("#btn-update").click(function (e) {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    })
    e.preventDefault();
    var formData = {
        name: $('#Getname').val(),
    }
    var sid = $('#search').val();
    console.log(formData);
    $.ajax({
        url: '{{ URL::to('update-data/')}}'+"/"+ sid,
        type: 'PUT',
        data: formData,
        dataType: 'json',
         success: function (response) {
            console.log(response);
            $('#Getname').val(response.name);
        },
    });
});

View:

<form method="POST" action="{{ route('data.store') }}">
  @method('PUT')
  @csrf
  <div>
    <label>ID No</label>
    <input type="text" id="search" class="form-control">
  </div>
  <div>
    <label>Name</label>
    <input type="text" name="name" id="Getname" class="form-control">
  </div>
<div>
    <button class="btn btn-success" input type="button" onclick="this.form.submit()">Save</button>
</div>
  <div>
    <button class="btn btn-info" type="button" id="btn-update">Update Record
    </button>
  </div>
</form>
glitchy
  • 161
  • 3
  • 12
  • I see `$("#btn-update")` and i see `id="btn-save"`. You sure you posted the relevant code ? – Gabriele Petrioli Sep 15 '19 at 17:03
  • Yes, forgot to change the id name it was previously btn-save, please check now – glitchy Sep 15 '19 at 17:07
  • Have a look over here: https://stackoverflow.com/questions/28143674/laravel-form-html-with-put-method-for-put-routes Using the PUT method. – Daniel Sep 15 '19 at 18:34
  • @Daniel I have already added method blade directive in my form, please check – glitchy Sep 15 '19 at 18:40
  • Maybe you should use the POST method in your ajax call and let laravel handle the PUT in your form. – Daniel Sep 15 '19 at 19:05
  • @Daniel No, it will not work, route and method should be same – glitchy Sep 15 '19 at 19:15
  • 1
    Well, where I look it tells me to [use POST with formData](https://laracasts.com/discuss/channels/laravel/ajax-formdata-and-put-fails). PUT is not supported and should be implemented like you do. Try serialization. Over here: https://stackoverflow.com/questions/35375468/method-not-allow-put-with-ajax-call-in-laravel-5-2 – Daniel Sep 15 '19 at 19:44
  • @Daniel Thanks for the links to refer first link worked for me, changes I made in Ajax `type: 'POST'` and sent method `PUT` in `data = { name: $('#Getname').val(), '_method':'PUT'}` – glitchy Sep 16 '19 at 15:43
  • Good. Happy you got it working. I added the answer so you can close this thread. – Daniel Sep 16 '19 at 16:17

2 Answers2

0

I m not sur but probably you are using put method bur in the form post method, may be it is conflicting. Try using post instead of put

Tanvir Ahmed
  • 969
  • 12
  • 24
  • I'm using same form but different buttons for Save and Update, currently I'm updating record hence used PUT method via Ajax also tried your solution still no luck. – glitchy Sep 15 '19 at 18:34
0

Referring to source https://laracasts.com/discuss/channels/laravel/ajax-formdata-and-put-fails

Configure the Ajax call to the back-end with a POST request and add the PUT method as a variable to the dataset.

$ajax({
    type: 'POST'
    url: "/path/to/route",
    data: {name: $('#Getname').val(), '_method':'PUT'},
    success: function(data) {
        console.log(data);    
    }
});
Daniel
  • 4,816
  • 3
  • 27
  • 31
  • No, make correction in your answer as follows, instead of `method: POST`, add `type: 'POST'` and in data callback add this `data = {name: $('#Getname').val(), '_method':'PUT'}` – glitchy Sep 16 '19 at 16:37
  • That's interesting. From the [jQuery docs](https://api.jquery.com/jquery.ajax/) (about halfway) the `method` and `type` are aliases. – Daniel Sep 16 '19 at 17:13