php 404http://127.0.0.1:8000/meusprocessos/processos/search/Anula(页面未找到)页面未找到

fnvucqvd  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(113)

我正在做一个关于laravel的项目,我有一个搜索输入,我使用jquery来搜索这个词。
但是我得到了下面的错误,但是我不知道我做错了什么。
404(未找到页面)
下面继续html:

@extends('adminlte::page')

@section('title', 'Dashboard')

@section('content')
    <link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
        <div class="col-md-9 mb-5">
            <div class="box-tools pull-right">
                <div class="has-feedback">
                    <form>
                        <input type="text" name="search" class="form-control input-sm" id="search" value="" placeholder="Procurar Processo pelo nome">
                       <!-- <a type="submit" class="btn btn-primary" id="processosSearch">Procurar</a> -->
                    </form>
                </div>
            </div>
            <div id='processos'>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.2.min.js"
            integrity="sha256-2krYZKh//PcchRtd+H+VyyQoZ/e3EcrkxhM8ycwASPA=" crossorigin="anonymous"></script>
    <script>

        $("#refreshProcessos").on('click', function () {
            $("#processos").html("<img src=' https://flevix.com/wp-content/uploads/2019/07/Curve-Loading.gif' >")
            $("#processos").load("{!! route('meusprocessos.processos') !!}")
        });

        $("#search").on("keyup", function() {
            var val = $.trim(this.value);
            //val = val.toLowerCase();
            //console.log(val);
            $("#processos").load("{!! route('meusprocessos.processosSearch') !!}"+ "/" + val)
        });

        $(document).on('click', 'a.readProcesso', function (e) {
            var id = $(this).attr("data-id")
            //console.log(id);
            $("#processos").html("<img src=' https://flevix.com/wp-content/uploads/2019/07/Curve-Loading.gif' >")
            $("#processos").load("{!! route('meusprocessos.processo') !!}" + "/" + id)
        });

        $("#processos").html("<img src=' https://flevix.com/wp-content/uploads/2019/07/Curve-Loading.gif' >")
        $("#processos").load("{!! route('meusprocessos.processos') !!}")

    </script>
@stop

这里的路线:

Route::get('meusprocessos/processos/search', [App\Http\Controllers\MeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');

此处为控制器:

public function processosSearch($val)
{
    echo $val;
    $search = $val;
    $processos = Processo::where('name', 'like', '%' . $search . '%')
        ->where('funcionario_id', \auth()->user()->id)
        ->get();
    $departamentos = Departamento::all();

    return view('meusprocessos.processosSearch')->with([
        'processos' => $processos,
        'departamentos' => $departamentos
    ]);
}

它根本不去看风景,但我不知道我做错了什么。这是我应用路线的方式有问题,但我不知道如何用不同的方式做。
我不认为需要其余的代码,但它在这里:
https://github.com/Maneneco/EmailManager

ztyzrc3y

ztyzrc3y1#

要修复代码,需要将$val变量添加到Route:

Route::get('meusprocessos/processos/search/{val}', [App\Http\Controllers\MeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');

话虽如此,我还是想推荐你在需要搜索路径的时候使用post请求,并且不是在url中传递搜索查询,而是在请求正文中传递。
大概是这样的
routes/web.php

Route::post('meusprocessos/processos/search', [App\Http\Controllers\MeusProcessosController::class, 'processosSearch'])->name('meusprocessos.processosSearch');

在jQuery搜索中:

$("#processos").post("{!! route('meusprocessos.processosSearch') !!}", { search: val });

然后在控制器中:

public function processosSearch(Request $request)
{
    $val = $request->search;
    echo $val;
    $search = $val;
    $processos = Processo::where('name', 'like', '%' . $search . '%')
        ->where('funcionario_id', \auth()->user()->id)
        ->get();
    $departamentos = Departamento::all();

    return view('meusprocessos.processosSearch')->with([
        'processos' => $processos,
        'departamentos' => $departamentos
    ]);
}

当然,不要忘记使用控制器顶部的请求:

use Illuminate\Http\Request;

相关问题