Didn't we just help with this?
Jul 18, 2022
16
Level 1
How to solve "undefined" in JavaScript?
I have a block of Published Yes and No.
<div class="card mb-3">
<div class="card-header">{{ __('Published') }}</div>
<div class="card-body">
<div class="form-check">
<input class="form-check-input published_filter" type="radio" name="published" id="yes" value="1">
<label class="form-check-label" for="yes">
Yes
</label>
</div>
<div class="form-check">
<input class="form-check-input published_filter" type="radio" name="published" id="no" value="0">
<label class="form-check-label" for="no">
No
</label>
</div>
</div>
</div>
When I click Yes and No, I see this alert undefined.
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(".published_filter").change(function(){
var publish = $(this).val();
$.ajax({
type:'POST',
url:"{{ route('category-filter') }}",
data:{publish: publish},
success:function(data){
alert(data.success);
}
});
});
</script>
Level 102
@esfer yes status is always success? You are setting it yourself?
Maybe you want
return response()->json([
'status' => $request->boolean('publish') ? 1 :0,
'data' => $products,
]);
success:function(data){
alert(data.status);
}
1 like
Please or to participate in this conversation.