Reputation: 117
If the return has data then the code works fine. But if it is empty it is supposed to show error log. But not happening
$.ajax({
type: "POST",
url: "<?php echo base_url();?>homecontroller/category_search_name",
data: {search_name: search_text},
success: function(data){
var mainObj = JSON.parse(data);
var j = 1;
var k = '<tbody>'
for(i = 0;i < mainObj.length; i++){
k+= '<tr>';
k+= '<td class="column1">' + j + '</td>';
k+= '<td class="column2">' + mainObj[i].s_name + '</td>';
k+= '<td class="column3">' + mainObj[i].s_admissionno + '</td>';
k+= '<td class="column4">' + mainObj[i].s_dob + '</td>';
k+= '<td class="column5">' + mainObj[i].s_address + '</td>';
k+= '</tr>';
j++;
}
k+='</tbody>';
document.getElementById('tbody').innerHTML = k;
},
error:function(jqXHR, textStatus, errorThrown){
alert("error");
}
});
Upvotes: 0
Views: 38
Reputation: 191
You can also refer to the following example in fiddle
enter code here
Taken the fiddle from following link
Upvotes: 0
Reputation: 349
Actually, the "error" parameter in ajax won't get triggered even if you get no results. The "error" parameter in ajax gets triggered only if the AJAX itself failed to execute due to some exceptions or something alike in code.
If you want to trigger "error" when no result is found, just return a parameter "countOfRecords" from backend check the value of it in "success" parameter of AJAX.
For example:
success: function(data){
var mainObj = JSON.parse(data);
if(mainObj.countOfRecords == 0) {
//Your "Error" code here
}
}
Upvotes: 1