【问题标题】:Error "POST http://127.0.0.1:8000/get-data-by-id 404 (Not Found)错误“POST http://127.0.0.1:8000/get-data-by-id 404(未找到)
【发布时间】:2022-01-14 18:34:12
【问题描述】:

晚上好,我是 Laravel 的新手,正在尝试一个用于内部网站的应用程序。 尝试将变量(BuffaloID)从视图传递到控制器并从数据库中获取该特定 ID 的数据,然后返回到视图,显示具有该特定 ID 数据的模式。 在做同样的事情时,我收到错误““POST http://127.0.0.1:8000/get-data-by-id 404(未找到)” 任何人都可以帮助我...在此先感谢

web.php 文件

Route::post('get-data-by-id/{buffaloid}',[BuffaloMonitorController::class,'getbuffaloidformonitor'])->name('getbuffaloidformonitor');
('getbuffaloidformonitor');

BuffalomonitorController 文件

     public function getbuffaloidformonitor(Request $req )
        {

            
            $inspectionbuffaloid = ($buffaloid);
            

            $viewinspectiondata = buffalomonitor::where ('buffaloID','=','$inspectionbuffaloid')->get(); // get data for view inspection data for selected buffaloID

            
            return view ('pages.Buffalo.BuffaloMonitor',['viewinspectiondata'=>$viewinspectiondata]);

            
        }
            
            
            
        }

BuffaloMonitor.blade.php

                   <script>

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

        // get data from table and show in View modal by Buffalo ID.


                $(document).on('click', '.viewmonitormodal', function() {

                    var modal_data = $(this).data('info').split(',');
                    
                    $('#viewbuffaloID').val(modal_data[1]);

                    var buffaloid = document.getElementById('viewbuffaloID').value // get buffalo id from textbox to get data for that ID
                    var token = "{{ csrf_token()}}";

                    alert(buffaloid);

                    $.ajax({
                        type                :'POST',
                        url                 :"{{ url('get-data-by-id') }}",
                        //dataType          : 'json',
                        data                :{buffalo_id:buffaloid, _token: token},

                        success:function(refbuffaloid){

                            alert(buffaloid);
                        
                        }
                    });

【问题讨论】:

    标签: laravel


    【解决方案1】:

    路线 尝试查看文档,因为您的路线应该是

    // and import the BuffaloController on the top like
    // use App\Http\Controllers\BuffalomonitorController
    
    Route::get('get-data-by-id/{id}',[BuffaloMonitorController::class, 'getbuffaloidformonitor')->name('getbuffaloidformonitor'); // remove anything after this
    

    控制者 应该知道您将传递它一个 id,因此将其作为参数输入

    public function getbuffaloidformonitor($BuffaloId, $Request $req)
            {
                return $BuffaloId;
            }
    

    作为一种常见做法,如果您将 ID 传递给路由,您应该将其设为 get 请求,然后像 url(.... "get-data-by-id" . $buffaloID) 一样发送请求。

                      $.ajax({
                            type                :'GET',
                            url                 :"{{ url('get-data-by-id') }}" + buffaloid // not sure about this routing but just add the buffaloid to the url
                            success:function(refbuffaloid){
    
                                alert(buffaloid);
                            
                            }
                        });
    

    【讨论】:

    • 感谢您的回复....它的工作,但有一个错误 - POST 127.0.0.1:8000/get-data-by-id 500(内部服务器错误),如果我禁用 - 返回 $BuffaloId;从控制器不会有错误......我错过了什么......
    • 是的,你在路由中添加了指向控制器时我错过的]
    • 是的...在路由中添加了“]”...谢谢........现在还有一点 - 公共函数 getbuffaloidformonitor(Request $req, $buffaloid=null) -如果我删除“null”,则错误为 500,并且在日志文件中 - local.ERROR: Too little arguments to function
    • 是的,因为如果您想删除$BuffaloID 的默认值,您应该配置路由以接受动态ID,例如Route::post('get-data-by-id/{TheID}', [BuffaloMonitorController::class])
    • 嗨..晚上好..抱歉延迟回复....我遇到了同样的错误......任何人都可以帮助我......跨度>
    【解决方案2】:

    您的路线需要您需要传递给控制器​​的参数,但控制器不知道该参数。您需要将其作为参数传递给控制器​​方法。

    路线:

    Route::post('get-data-by-id/{buffaloid}',[BuffaloMonitorController::class,'getbuffaloidformonitor'])->name('getbuffaloidformonitor');
    

    控制器(清理了一下}:

    public function getbuffaloidformonitor(Request $request, string $buffaloid )
    {
        $viewinspectiondata = buffalomonitor::where('buffaloID', (int)$buffaloid)->get();
    
        return view ('pages.Buffalo.BuffaloMonitor' ['viewinspectiondata'=>$viewinspectiondata]);
        
    }
            
    

    【讨论】:

    • 你好.....抱歉延迟回复......我完全迷失了............出现错误“GET 127.0.0.1:8000/get-data-by-id?buffaloid=Buffalo-01404(未找到)”但是当我更换?与/然后链接是工作.....你能帮我我在哪里犯了错误....抱歉延迟和提前感谢
    猜你喜欢
    • 2021-06-19
    • 2019-08-14
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 2023-01-08
    • 2021-02-27
    相关资源
    最近更新 更多