mercredi 30 août 2017

Laravel 5.4 - Jquery Ajax Method post does not exist

I am getting this error Method post does not exist., and I can't figure it out what is going on. My routes:

Route::group(['middleware' => ['auth']], function () {
    Route::get('/', 'SiteController@index');
    Route::get('/dashboard', 'DashboardController@index')->middleware('role');

    Route::post('/search', 'UserController@search')->middleware('role');
});

HTML code:

<form class="navbar-form" role="search" id="employees-search">

    <div class="input-group add-on col-md-6">

    <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i>    </button>
    </div>
        <input class="form-control" placeholder="Search" name="employee-name" id="employee-name" type="text">

    </div>
</form>

JS code:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

// Search users

$('#employees-search').on('keyup', '#employee-name', function(){


    let
        $this = $(this),
        name  = $this.val();

    if(name.length > 2){

        $.ajax({
            url: '/search',
            type: 'POST',
            data: $('#employees-search').serialize(),
            contentType: 'application/json',
            dataType: 'json',
        })
        .done(function() {
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });

    }


})

UserController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function search(Request $request)
    {
        dd($request->post('employee-name'));
    }
}

What am I doing wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire