我正在尝试使用jQuery的get()方法获取数据以填充下拉列表,但我收到500内部服务器错误。我的jQuery是:
$(document).ready(function(){
$('#cat').change(function(){
$.get("{{url('types')}}",{id:$(this).val()}, function(data){
$('#subcat').empty();
$.each(data, function(index, element) {
$('#subcat').append("<option value='"+ element.ty_id +"'>" + element.ty_name + "</option>");
});
});
});
});
路线是:
Route::get('types', function(){
$cat_id = Input::get('id');
$type = App\type::where('cat_id', $cat_id)->get();
return Response::json($type);
});
sample.blade.php
文件是:
<form method="#"><input type="hidden" name="_token" value="{{ csrf_token() }}">
<label>Categories</label>
<select class="form-group" id="cat" name="cat">
@foreach($cat as $val)
<option value="{{$val->cat_id}}">{{$val->cat_name}}</option>
@endforeach
</select><br>
<label>type</label>
<select class="form-group" name="subcat" id="subcat">
<option value=""></option>
</select>
</form>