显示来自 ajax 结果的 json_encode 数组

Display json_encode array from ajax result

我有这个结果

{"policy":[{"id":"1","policy_name":"Policy 1","description":"Testing","status":"Active","valid_until":"2022-05-18","tags":"Test","active":"0","date_added":"2022-05-18 05:36:02"}]}

我想显示数组中的 policy_name,我尝试使用此 alert(response['policy'].policy_name); 来提醒它,但出现错误。

43:4801 Uncaught TypeError: Cannot read properties of undefined (reading 'policy_name')

已更新 这是我的全部代码:

AJAX

$("*[id^='pol_action']").each(function() {
            $(this).change(function(){ 
                var value = $(this).val();
                var id = $('option:selected', this).attr('r-id');
               

                if($(this).val() == 'edit')
                {
                    $.ajax({
                        url: "<?php echo base_url();?>admin/leads/getPolData",
                        type: "POST",
                        data: {id: id},
                        success: function(response){
                            
                        $('#update_policy_modal').modal('show');
                            alert(response['policy'].policy_name);
                            console.log(response);
                        },
                            error: function(data){
                                console.log('error');
                            }
                    });
                }
                else if ($(this).val() == 'delete')
                {
                    
                }
            });
        }); 

控制器

public function getPolData()
    {
        $id = $this->input->post('id');
        
        $policy = $this->leads_model->getDataPol($id);
        $this->page_data['policy'] = $policy;

        echo json_encode($this->page_data);
    }

型号

public function getDataPol($id)
    {
        $where = array(
            'id'       => $id,
          );

        $this->db->select('*');
        $this->db->from('tblpolicies');
        $this->db->where($where);
        $query = $this->db->get();
        return $query->result();
    }

你能帮我解决这些问题吗?提前谢谢你。

在您的 Ajax 调用中,尝试 json 解析然后提醒字段:

$.ajax({
          url: "<?php echo base_url();?>admin/leads/getPolData",
          type: "POST",
          data: {id: id},
          success: function(response){
                $('#update_policy_modal').modal('show');
                var obj = $.parseJSON(response);
                alert(obj.policy[0].policy_name);
                console.log(response);
          },
          error: function(data){
                console.log('error');
          }
      });

我编辑了答案,下面是 Javascript:

var json = '{"policy":[{"id":"1","policy_name":"Policy 1","description":"Testing","status":"Active","valid_until":"2022-05-18","tags":"Test","active":"0","date_added":"2022-05-18 05:36:02"}]}';

const obj= JSON.parse(json);

console.log(obj.policy[0].policy_name);

您正在访问一个数组,因此它应该是:

response['policy'][0].policy_name

您可以尝试添加 dataType: "JSON" 这样您就不必解析 JSON,如下所示。然后你可以尝试遍历数据数组:

$.ajax({
   url: ...,
   type: "POST",
   data: {id:id},
   dataType: "JSON"
   success: function(data){
      $.each(data, function(key, value){
          alert(value.policy_name);
      });
   }
});