My AJAX code is like below
$('select[id="state"]').on('change', function() {
var stateID = $(this).val();
alert(stateID); // I am getting value here
if(stateID) {
$.ajax({
url: '/cities?code='+stateID,
type: "GET",
dataType: "json",
success:function(data) {
alert('hello');
$('select[name="city"]').empty();
$.each(data, function(key, value) {
$('select[name="city"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="city"]').empty();
}
});
My route is like below
Route::get('countries/{countryCode}/cities/{id}', 'Ajax\LocationController@getSelectedCity');
My controller function is like below
public function getSelectedCity($countryCode, $cityId)
{
// Get the City by its ID
$cacheId = $countryCode . '.city.with.admins' . $cityId;
Log::info($cityId); // Value is not logging here. No log file generate for this.
$city = Cache::remember($cacheId, $this->cacheExpiration, function () use ($countryCode, $cityId) {
return City::countryOf($countryCode)->with(['subAdmin1', 'subAdmin2'])->where('id', $cityId)->first();
});
if (!empty($city)) {
$text = $city->name;
if (isset($city->subAdmin2) && !empty($city->subAdmin2)) {
$text .= ', ' . $city->subAdmin2->name;
} else {
if (isset($city->subAdmin1) && !empty($city->subAdmin1)) {
$text .= ', ' . $city->subAdmin1->name;
}
}
$cityArr = ['id' => $city->id, 'text' => $text];
} else {
$cityArr = ['id' => 0, 'text' => t('select_a_city', [], 'global', request()->get('languageCode'))];
}
return response()->json($cityArr, 200, [], JSON_UNESCAPED_UNICODE);
}
I used use Illuminate\Support\Facades\Log; at the start of the controller.
How to debug $.ajax() in Laravel 7 ?