使用AJAX函数访问JSON元素

时间:2018-05-24 18:23:06

标签: javascript php jquery json ajax

我需要从我的codeigniter控制器获取数据并将其作为ajax调用发送到我的视图。所以我使用这个函数将数据发送到ajax函数

function vendorAccess(){
    $result = $this->Admin_model->allvendor();

     foreach($result as $row){
        $data = $row["joinedDate"];

        $now = time(); 
        $your_date = strtotime($data);
        $datediff = $now - $your_date;

        $cont =  round($datediff / (60 * 60 * 24));
         if($cont == 85){
              $response['vendorName'] = $row['vendorName'];
              echo json_encode($response);
        }

     }

}

这会将数据发送到我的ajax函数。我的ajax功能是这个

$(document).ready(function(){
 $.ajax({
     url: '<?php echo base_url();?>/Admin/vendorAccess',
     type: 'get',
     dataType:'text',
     success: function(res){
         console.log(res);

     }
 });    
});

使用此代码我得到输出

  

{&#34; vendorName&#34;:&#34; Cinnamon Grand&#34;} {&#34; vendorName&#34;:&#34; Saloon Bhagya&#34;}

现在我需要使用jquery作为单独的两个名称将这两个vendorName输入到我的div中。我在stackoverflow上搜索了这个问题,我找到了一些解决方案。但这些解决方案对我不起作用。下面是一个例子

$(document).ready(function(){
    //$('#numOfNot').html('New');
    var tmp=null;
     $.ajax({
         url: '<?php echo base_url();?>/Admin/vendorAccess',
         type: 'get',
         dataType:'json',
         success: function(res){
             alert(res.vendorName);

         }
     });    
 });

当我使用它时,请给我提示undefined。 非常感谢您花费大量时间来解决我的问题

2 个答案:

答案 0 :(得分:2)

你需要在控制器中填入一个数组,而不是每次循环都回显JSON。

function vendorAccess(){
    $result = $this->Admin_model->allvendor();
    $names = array();
    foreach($result as $row){
        $data = $row["joinedDate"];

        $now = time(); 
        $your_date = strtotime($data);
        $datediff = $now - $your_date;

        $cont =  round($datediff / (60 * 60 * 24));
        if($cont == 85){
            $names[] = $row['vendorName'];
        }
    }
    echo json_encode(array('vendorName' => $names));
}

答案 1 :(得分:0)

我建议进行一些重构,以使其正常工作。您返回的JSON无效。您需要在PHP方法中创建一个对象数组,然后在foreach()循环之外,echo使用json_encode()方法将此数据返回到屏幕

<强> PHP:

function vendorAccess(){
    $result = $this->Admin_model->allvendor();
    $vendors = [];

    foreach($result as $row){
        $data = $row["joinedDate"];  
        $now = time(); 
        $your_date = strtotime($data);
        $datediff = $now - $your_date;

        $cont =  round($datediff / (60 * 60 * 24));

        if($cont == 85){
            $vendors[] = ['vendorName' => $row['vendorName']];
        }
    }

    echo json_encode($vendors);
}

然后,在JavaScript中,您需要检查以确保您正在接收数组,然后循环遍历数组并以某种方式使用每个索引处的数据。在这种情况下,我使用console.log()将数据记录到控制台,但您可以在此处执行任何其他逻辑:

<强> JavaScript的:

$(document).ready(function() {
    var tmp=null;

    $.ajax({
        url: '<?php echo base_url();?>/Admin/vendorAccess',
        type: 'GET',
        dataType:'json',
        success: function(res) {
            if(res && res.length) {
                for(var a = 0, len = res.length; a < len; a++) {
                    console.log(res[a].vendorName);
                }
            }
        }
    });    
});