首页 > 解决方案 > Ajax 数据未显示在控制台日志中,但给出错误“http://localhost/IFICSV3/public/sla/sla/getbranch/180 not found”

问题描述

我尝试使用 ajax 使下拉选项依赖。我想在控制台中查看数据是否成功。我希望数据显示在控制台日志中,但给出错误

http://localhost/fic/public/sla/sla/getbranch/180 404 未找到

这是我查看 ajax 的代码

if(country_id) {

    //console.log(country_id);

    $.ajax ({
        url: 'sla/getbranch/'+country_id,
        //url: "{{ route('sla.getbranch') }}"+country_id,
        type: 'GET',
        datatype: 'json',
        success: function(data){
            console.log(data);
        }
    })

我的控制器

public function index()
{
    $custList = $this->hdcustomermaster->getAllCustomerActiveSts();
    return view('sla.slm.SLAList', compact('custList'));
}

public function getbranch($id)
{
    $data=HDCustomerBranch::where('cb_customer_ID',$id)->pluck('cu_customer_ID','cu_customer_Name');
    return json_encode($data);
}

我的 web.php

Route::group(['prefix' => 'sla'], function () {

   Route::get('/','SlaController@index');
   Route::resource('sla', 'SlaController');
   Route::get('sla/getbranch','SlaController@getbranch')->name('sla.getbranch');
});

标签: javascriptphpajaxlaravel

解决方案


像这样替换您的路线

Route::group(['prefix' => 'sla'], function () {

   Route::get('/','SlaController@index');
   Route::resource('sla', 'SlaController');
   Route::get('sla/getbranch/{id}','SlaController@getbranch')->name('sla.getbranch');
});

这里的问题是您在该路线中将 180 作为参数传递。但是您没有在您的路线中声明该参数。您也可以添加一个“?” 如果它是一个可选参数,像这样

Route::get('sla/getbranch/{?id}','SlaController@getbranch')->name('sla.getbranch');

推荐阅读