【问题标题】:Jquery ajax POST 500 internal server errorJquery ajax POST 500 内部服务器错误
【发布时间】:2019-12-10 15:21:07
【问题描述】:

我不断收到加载资源失败:服务器响应状态为 500(内部服务器错误),表明对我的控制器方法的调用不起作用,我不知道为什么:

<script type="text/javascript">
    $('.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('tambah_aktivitas.fetch') }}",
              method:"POST",
              data:{select:select, value:value, _token:_token, dependent:dependent},
              success:function(result)
              {
               $('#'+dependent).html(result);
              }

             })
            }
           });



           $('#nik').change(function(){
            $('#username').val('');
            $('#kategori3').val('');


           });

           $('#kategori2').change(function(){
            $('#kategori3').val('');
           });

             $("select").val();



  </script>

我的控制器

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

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 GET 方法。支持的方法:POST。

【问题讨论】:

    标签: jquery ajax laravel


    【解决方案1】:

    由于您有一个到 localhost 的链接(提示:没有人可以看到或使用该链接),我将假设您使用 xampp 作为服务器。

    您的服务器拒绝您访问的原因不可能有多种,这里有几个解决方案:

    /Applications/xampp/etc/httpd.conf 中有一个名为 httpd.conf 的文件,打开此文件并执行以下操作:

    改变: AllowOverride AuthConfig AllowOverride All

    将admin文件夹的权限改为“755”

    【讨论】:

    • 如果我单击该链接,显示消息' Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 此路由不支持 GET 方法。支持的方法:POST。 '
    • 那是因为您正在运行服务器。您正在运行的 xampp 服务器只允许您和有权访问您的 Internet 连接的人查看内容。如果你去 Xampp 控制面板,找到 apache,点击停止,然后点击链接,你会看到其他人看到的。
    • 由于不支持“GET”方法,这意味着服务器正在主动拒绝您访问,请尝试让您访问服务器以获取某些内容
    【解决方案2】:

    我相信您在查询中缺少选择。

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

    【讨论】: