【问题标题】:Laravel - Ajax call not respondingLaravel - Ajax 调用没有响应
【发布时间】:2019-08-09 13:06:42
【问题描述】:

我正在尝试从下拉框中发布数据并使用 Ajax 发送请求。它在本地主机上运行良好,但在共享主机上无法正常运行。我有 3 个保管箱,值根据从其父保管箱中选择的值而变化。例如:汽车制造商(奥迪)-> 车型->(A4、A5、Q5 等奥迪车型)->年份(19XX - 2019)

我只能选择品牌,但无法从所选汽车品牌中获取任何数据。

home.blade.php

<div class="form-group">

  <select   class="form-control dynamic"  name="Make"  id="Make" data-dependent='Model'>
    @foreach($carLists as $carMake)
    <option value="{{$carMake->Make}}">{{$carMake->Make}} </option>

    @endforeach
  </select>
</div>

<div class="form-group">

  <select   class="form-control dynamic"  name="Model" id="Model"  data-dependent='Year'>
    <option value="">Select Model</option>
  </select>
</div>


<div class="form-group">

  <select   class="form-control dynamic" name="Year" id="Year"  data-dependent='Body'>
    <option value="">Select Manufacturing year</option>
  </select>
</div>

<script>
$(document).ready(function(){

 $('.dynamic').change(function(){
  if($(this).val() != '')
  {
   var select = $(this).attr("id");
   var value = $(this).val();
   var dependent = $(this).data('dependent');
   var _token = $('input[name="_token"]').val();
   $.ajax({
      url:"{{ route('pagescontroller.fetch') }}",
    method:"POST",
    data:{select:select, value:value, _token:_token, dependent:dependent},
    success:function(result)
    {
     $('#'+dependent).html(result);
    }

   })
  }
 });

 $('#Make').change(function(){
  $('#Model').val('');
  $('#Year').val('');
   $('#Make').val($(this).val());
   console.log($('#HidMake'));
 });

 $('#Model').change(function(){
  $('#Year').val('');
 });

});
</script>

PageController.php

class PagesController extends Controller
{

          function fetch(Request $request)
          {
           $select = $request->get('select');
           $value = $request->get('value');
           $dependent = $request->get('dependent');
           $data = DB::table('carLists')
             ->where($select, $value)
             ->groupBy($dependent)
             ->get();
           $output = '<option value="">Select '.ucfirst($dependent).'</option>';
           foreach($data as $row)
           {
            $output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
           }
           echo $output;
          }

路由(web.php)

Route::post('inc/sidebar/fetch', 'PagesController@fetch')->name('pagescontroller.fetch');

我正在尝试很多,但不知道这里出了什么问题,但是,在本地主机上它工作正常。

感谢您的时间。

【问题讨论】:

  • 你在ajax响应中得到的意思是404或其他东西。检查您的浏览器网络选项卡或控制台
  • 一个object { }
  • 在你的控制器函数中回显某些东西并死掉;它
  • 我还得到:源映射错误:请求失败,状态为 404 资源 URL:stagingcars.com/local/bootstrap.css 源映射 URL:bootstrap.min.css.map[了解更多]
  • 这不是它只是一个警告的原因。

标签: javascript ajax laravel laravel-5


【解决方案1】:

尝试将网址更改为

var url = APP_URL + '/inc/sidebar/fetch';

并设置标题

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

而不是

 var _token = $('input[name="_token"]').val();

然后告诉我,你得到的结果。

【讨论】:

  • 嗨,我找到了问题所在。问题是表名。我在控制器中拼错了表名。但是感谢您的帮助
猜你喜欢
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 2018-03-02
  • 2012-04-25
相关资源
最近更新 更多