我正在使用CodeIgniter。我正在使用AJAX从数据库中获取记录,并使用JSON在视图页面上显示数据。一切都在进行中。
现在,当我获取空的JSON数据时,它将在我的PHP页面上显示“找不到数据”,并显示在network tab
中。但是我无法检查AJAX中的空数据以在查看页面上显示该消息。
控制器
if (empty($result) || count($result)==0){
echo "No data found";
// return 0;
}
else
{
foreach ($result as $row)
{
$arr_result[] = array(
"name" => $row->firstname.' '.$row->lastname,
"mobileno" => $row->mobileno,
"email_id" => $row->email_id
);
}
echo json_encode($arr_result);
}
Custom.js
$.ajax({
url: baseUrl + "/Employee_control/search_addSalaryrecord",
method: "POST",
data: {employee_name: employee_name,crm_id:crm_id,employee_id:employee_id},
success: function(response) {
//alert(response);
var data = JSON.parse(response);
if (data.status == 'error')
{
alert(data.msg);
}
else{
$('.personel_info').show();
var trHTML = '';
$.each(data, function (i, o){
$('#name_emp').text(o.name);
$('#mobileno_emp').text(o.mobileno);
$('#employee_email').text(o.employee_email);
});
}
}
});
在控制台中,我收到错误消息
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data[Learn More](I know my response is empty that the reason I am getting this error)
位于var data = JSON.parse(response);
我尝试过
if (json.success) {
alert('All working');
}
else{
alert('no data');
}
but still getting the same issue.