获取ajax下拉基于父级id返回未定义的Laravel

时间:2018-01-25 01:06:03

标签: javascript php ajax laravel

我尝试获取所选city的{​​{1}}列表,并将城市名称返回为state(province)

这是我的控制器方法

undefined

我的刀片代码:

public function index(Request $request) {
        $rajaongkir = new Rajaongkir\Domestic('xxxxxxxxxxxxxxx');
        $province = $rajaongkir->province();

        $origin = 152; //Jakarta
        $destination = 178; // Kediri
        $weight = 1;
        $courier = 'tiki'; // jne, tiki, pos
        $cost = $rajaongkir->cost($origin, $destination, $weight, $courier);
        return view('front.raja', compact('province', 'cost'));
    }

    public function getCityList($province)
    {
        $rajaongkir = new Rajaongkir\Domestic('xxxxxxxxxxxxxxx');
        $city = $rajaongkir->city($province);
        return response()->json($city);
    }

我的javascript代码:

<form action="">
                {{csrf_field()}}
                <select name="province" id="">
                    <option class="form-control" value="">Select Province</option>
                    @foreach ($province->data as $info)
                        <option value="{{$info->province_id}}">{{$info->province}}</option>
                    @endforeach
                </select>

                <select name="city" id="">
                    <option class="form-control" value="">Select City</option>
                </select>

                <div style="margin-top: 30px;">
                    Destination:
                        {{$cost->meta['destination']->province}},
                        {{$cost->meta['destination']->type}},
                        {{$cost->meta['destination']->city_name}},
                        {{$cost->meta['destination']->postal_code}}.
                    @foreach($cost->data as $option)
                          <h3>{{$option->code}} <small>{{$option->name}}</small></h3>
                           @foreach($option->costs as $cost)
                                 @foreach($cost->cost as $c)
                                     <label class="radio">
                                        <input name="post" value="{{ $c->value }}" type="radio">{{$cost->service}} / {{ $c->value }} Rp - {{ $c->etd }} hari @if(!empty($c->note))- {{ $c->note }} @endif
                                    </label>        
                                @endforeach             
                          @endforeach
                    @endforeach
                </div>
                <button type="submit" class="btn btn-default">save</button>
            </form>

这是我的代码的结果:

screen1

  

如果我在<script type="text/javascript"> $(document).ready(function() { $('select[name="province"]').on('change', function() { var provinceID = $(this).val(); if(provinceID) { $.ajax({ url: '{{ url('get-city-list') }}/'+encodeURI(provinceID), type: "GET", dataType: "json", success:function(data) { $('select[name="city"]').empty(); $.each(data, function(key, value) { $('select[name="city"]').append('<option class="form-control" value="'+ value['city_id'] +'">'+ value['city_name'] + ' - ' + value['type'] +'</option>'); }); } }); }else{ $('select[name="city"]').empty(); } }); }); </script> 之前添加alert(JSON.stringify(value, null, 4));   返回:

screen2

以下是我尝试实现的总计append

  1. 选择省
  2. 获取该省的城市列表
  3. 获取该城市的ID并将其作为我的Logic添加到索引函数$destination
  4.   

    关键是ajax发生了这一切,为什么?因为我试着用   这个表格在结账前在我的购物车中,所以当我点击结账按钮时   已包含运费。

1 个答案:

答案 0 :(得分:2)

查看您的评论,您似乎正在尝试迭代错误的对象:

使用:

$.each(data.data

而不仅仅是:

 $.each(data

这是因为ajax成功的data回复为:

{
    code:200,
    data:[
        ...etc.
    ],
    error: ...etc.
}