Api 有解码数据但不能在表格中工作

Api have decode data but not work in form

这是我的 create.blade.php 文件,其中包含 ajax 函数 API 和 HTML 标记代码..

@extends('layouts.app')

@section('content')
       <link rel="stylesheet" href="http://www.codermen.com/css/bootstrap.min.css">    
    <script src="http://www.codermen.com/js/jquery.js"></script>
<form enctype="multipart/form-data" method="post" action="{{route('post.store')}}" >
                @csrf

<div class="form-group col-md-8">
                    Category<select name="category" id="category" class="form-control">
                                    <option>select</option>
                        @foreach($categories as $category)
                <option value="{{$category->id}}">{{$category->category}}</option>
                @endforeach
                            </select>
                </div>
                <div class="form-group col-md-8">
                    Category<select name="subcategory" id="subcategory" class="form-control">
                                    <option>select</option>
                        @foreach($subcategories as $subcategory)
                <option value="{{$subcategory->id}}">{{$subcategory->subcategory}}</option>
                @endforeach
                            </select>
                </div>
</form>

<script type="text/javascript">
      $(document).ready(function() {
       
                        $('#category').change(function(){
                var categoryID = $(this).val();
                if(categoryID){
                    $.ajax({
                        type:"GET",
                        url:"{{url('/api/getSubcategory/')}}/"+categoryID,

                        success:function(res){

                            if(res){
                                $("#subcategory").empty();
                                $("#subcategory").append('<option>Select</option>');
                                $.each(res,function(key,value){
                                    $("#category").append("<option value='"+key+"'>"+value+"</option>");
                                });

                            }else{
                                $("#subcategory").empty();
                            }
                        }
                    });
                }else{
                    $("#subcategory").empty();
                }
            });
</script>
@endsection

这是我的控制器代码,包括创建函数和 getSubcategory 函数

public function create(){
    $categories = Category::all();
  $subcategories = Subcategory::all();
    return view('post.create', compact('categories', 'subcategories'));
}


 public function getSubcategory(Request $request){

    $id = $request->id;
    $subcategories = Subcategory::where('category_id',$id)->select
    ('Subcategory','id')->get();

      {{ dd(json_decode($subcategories, true)); }}
      // return view('post.create')->with('subcategories', json_decode($subcategories, true));

}

这是我的web.php

Route::get('/post/create', 'PostController@create')->name('post.create');
Route::get('api/getSubcategory/{id}', 'PostController@getSubcategory' );

现在我的问题是API有数据给我

但是当我打开此页面时 select 类别仍然没有显示相关的子类别

改变

 $subcategories = Subcategory::where('category_id',$id)->select
('Subcategory','id')->get();

 $subcategories = Subcategory::where('category_id',$id)->select
('Subcategory','id')->pluck('subcategory', 'id');

然后

return json_encode($subcategories);

可能需要在您的 ajax 成功函数中对其进行解码 - 两者都试试。