我想在Laravel Ajax搜索中显示'No Results Found'。在我的例子中,如果没有记录,它会显示一个空白屏幕。所以我想改成"No results found."有人能帮帮我吗?
<视图/strong>
<div class="row" style="text-align: center;padding-top: 25px;">
<div class="col-md-8" >
<h2 id="dynamic-row"></h2>
</div>
</div>
<script>
$('body').on('click' ,'#search' , function(){
var from = $('#from').val();
var to = $('#to').val();
$.ajax({
method: 'POST',
url: '{{ route("fare.search") }}',
dataType: 'json',
data: {
'_token' : '{{ csrf_token() }}',
from : from,
to: to,
},
success: function(res){
var tableRow = '';
$('#dynamic-row').html('');
$.each(res , function(index,value){
tableRow = '<h2>RS '+value.fare+'/-</h2>';
$('#dynamic-row').append(tableRow);
//$('#dynamic-row').css('font-weight', 'bold');
})
}
})
})
</script>
在" No result found "下返回404在控制器。
return response()->json('No Result Found', 404);
并在ajax调用中添加error函数:
$('body').on('click' ,'#search' , function(){
var from = $('#from').val();
var to = $('#to').val();
$.ajax({
method: 'POST',
url: '{{ route("fare.search") }}',
dataType: 'json',
data: {
'_token' : '{{ csrf_token() }}',
from : from,
to: to,
},
success: function(res){
var tableRow = '';
$('#dynamic-row').html('');
$.each(res , function(index,value){
tableRow = '<h2>RS '+value.fare+'/-</h2>';
$('#dynamic-row').append(tableRow);
//$('#dynamic-row').css('font-weight', 'bold');
})
},
error: function(res){
console.log(res);
}
})
})