我正在一个项目中,我要保存办公室地址并创建办公室。blade的国家和城市依赖下拉列表用于依赖下拉列表,我使用JavaScript进行了以下代码的AJAX调用,但是当我正在运行它,却收到“路由未在office.create.blade中定义错误。
下面是office.create.blade的
@section('scripts')
<script type="text/javascript">
$("#country").change(function(){
$.ajax({
url: "{{ route('admin.cities.get_by_country') }}?country_id=" + $(this).val(),
method: 'GET',
success: function(data) {
$('#city').html(data.html);
}
});
});
</script>
@endsection
<div class="panel panel-default">
<div class="panel-heading">
@lang('quickadmin.qa_create')
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('country_id', trans('quickadmin.offices.fields.country').'*', ['class' => 'control-label']) !!}
{!! Form::select('country_id', $countries, old('country_id'), ['class' => 'form-control select2', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('country_id'))
<p class="help-block">
{{ $errors->first('country_id') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('city_id', trans('quickadmin.offices.fields.city').'*', ['class' => 'control-label']) !!}
<select name="city_id" id="city" class="form-control">
<option value="">{{ trans('quickadmin.qa_please_select') }}</option>
</select>
<p class="help-block"></p>
@if($errors->has('city_id'))
<p class="help-block">
{{ $errors->first('city_id') }}
</p>
@endif
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('address', trans('quickadmin.offices.fields.address').'*', ['class' => 'control-label']) !!}
{!! Form::text('address', old('address'), ['class' => 'form-control', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
@if($errors->has('address'))
<p class="help-block">
{{ $errors->first('address') }}
</p>
@endif
</div>
</div>
</div>
</div>
CitiesController.php代码
public function get_by_country(Request $request)
{
abort_unless(\Gate::allows('city_access'), 401);
if (!$request->country_id) {
$html = '<option value="">'.trans('quickadmin.qa_please_select').'</option>';
} else {
$html = '';
$cities = City::where('country_id', $request->country_id)->get();
foreach ($cities as $city) {
$html .= '<option value="'.$city->id.'">'.$city->name.'</option>';
}
}
return response()->json(['html' => $html]);
}
OfficeController.php代码
public function perma_del($id)
{
if (! Gate::allows('office_delete')) {
return abort(401);
}
$office = Office::onlyTrashed()->findOrFail($id);
$office->forceDelete();
return redirect()->route('admin.offices.index');
}
如下所述在web.php中定义路由
Route::get('cities/get_by_country', 'CitiesController@get_by_country')->name('admin.cities.get_by_country');
我不知道我在哪里犯了错误? 期待帮助我扔这个 谢谢
答案 0 :(得分:0)
您可以先尝试使用原始网址,但请确保在网址之前指定前斜杠,例如:/ cities / get_by_country。
这会将网址视为基于root。