RouteCollection.php第161行中的NotFoundHttpException:laravel

时间:2017-01-25 15:12:06

标签: php ajax laravel laravel-5.3

我发送了一个ajax请求,我已经给它发了路,但它仍然没有找到。

这就是我正在做的事情

web.php

Route::post('/check_username/{username}', RegisterController@check_user_username');

RegisterController.php

public function check_user_username($username)
{
    $uername = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($username))))));
    $user = new User;
    $user = $user->where(['username','LIKE',$username])->get();

    return $user;
}

ajax请求

$(document).ready(function()
{
$("#username").blur(function(){
    //$(this).css("background-color", "red");
    var username = $(this).val();
    $.ajax(
    {
        url: '/check_username/',
        method: 'POST',
        data: {"username":username},
        success: function(data)
        {
            console.log(data);
        }
    });
});
});

有什么问题,还有什么可以让它发挥作用?

1 个答案:

答案 0 :(得分:1)

路线应如下所示:

Route::post('check_username', RegisterController@check_user_username');

方法:

public function check_user_username(Request $request)
{
    $user = User::where('username', 'like', '%'.$request->username.'%')->first();
    return response()->json($user);
}