Laravel 5.4 - Jquery Ajax方法帖子不存在

时间:2017-08-30 12:43:42

标签: laravel laravel-5 laravel-5.4

我收到此错误方法帖子不存在。,我无法弄明白发生了什么。我的路线:

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代码:

$.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'));
    }
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

Illuminate\Http\Request类没有post()方法。您正在寻找input()

dd($request->input('employee-name'));