如何检查JSON数据是否已从PHP中清空并显示消息?

时间:2018-07-15 03:21:32

标签: php json ajax codeigniter-3

我正在使用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.

0 个答案:

没有答案