简体   繁体   English

$ .each仅显示json的第一个值,而其他值在ajax成功中显示为未定义。

[英]$.each showing only first value of json and other values are shown as undefined in ajax success.?

hi guys i am trying to get the value of json arrays in $.each and get there values passed to another ajax the value is being sent from controller to view but can't show the values using $.each function. 嗨,大家好,我试图在$ .each中获取json数组的值,并将那里的值传递到另一个ajax,该值正从控制器发送到视图,但无法使用$ .each函数显示这些值。 let me show you my code to explain what is the problem 让我向您展示我的代码以说明问题所在

    function getfriend_requests()
    {
    var id=$('.id_data').attr('value');

    jQuery.ajax({
                    type:'POST',
                    url:'<?php echo base_url("user/getallfriends"); ?>',
                    data:{id:id},
                    dataType:'json',
                    success:function(data)
                    {   
                        console.log(data);
                            var ParsedObject = JSON.stringify(data);
                        var friend_request_data= $.parseJSON(ParsedObject);

                          $.each(friend_request_data, function(key,data) 
                            {

                              var uname=data.uname;
                              var senders_id=data.senders_id;
                               alert(senders_id);
                              var recievers_id=data.recievers_id;
                              var request_status=data.request_status;
                               alert(recievers_id);
                               alert(uname);
                              jQuery.ajax({
                            type:'POST',
                            url:'<?php  echo base_url("user/get_friends_Data"); ?>',
                            data:{senders_id:senders_id,recievers_id:recievers_id},
                            dataType:'json',
                            success:function(data)
                            {
                                   console.log(data); 
                                    var ParsedObject = JSON.stringify(data);
                                     var sender_values = $.parseJSON(ParsedObject);
                                // var senders_name=sender_values.sender_data;
                                // alert(senders_name);
                                var id=$('.id_data').attr('value');
                                    $.each(sender_values, function(key,data) 
                       {
                        var uname = data.uname;
                        var senders_id=data.id;

                        alert(uname);
                        alert(senders_id);
                        // $('.senders_div').attr('senders_id', $senders_id);
                        // if(senders_id==id)
                        // {

                        // }
                        // else{
                        //    if(uname){  
                        //     alert(request_status);
                        //     if(request_status=='pending')
                        //     {
                        //       //alert('hello');
                        //     }else{
                        //       //alert('data');
                        //     }

                            $('.friends_sidebar').append('<div>'+uname +'</div>');




                         });
                            //       $.each(sender_values,function,(key,data)
                            //       {
                            //         var uname = data.uname;
                            //          if(uname){  
                            // $('.friends_sidebar').html('<div>'+uname +'has sent you a request</div>');

                            //  }
                            //       });
                              }
                        });
                            });
                    }
                });
    }

now in this code when i try to use 'friend_request_data' it shows undefined alerting values all shows undefined. so i cant send the values to the second ajax that i am using 
let me also show you the controller 

        public function getallfriends()
    {
      $id=$this->input->post('id');
      $this->load->model('Pmodel');
      $data['senders']=$this->Pmodel->get_all_user_friends_sender($id);


      $data['recievers']=$this->Pmodel->get_all_user_friends_reciever($id);

      echo json_encode($data);
    }
    public function get_friends_Data()
    {
       $senders_id=$this->input->post('senders_id');
       $user_id = $this->session->userdata('user_id');
        $recievers_id=$this->input->post('recievers_id');
        $this->load->model('Pmodel');

    if($user_id==$senders_id)
    {

    }else{
      $userdata['senders_data']=$this->Pmodel->getUserdata($senders_id);

    }

    if($user_id==$recievers_id)
    {

    }
    else{
      $userdata['recievers_data']=$this->Pmodel->getUserdata($recievers_id);
    }

    echo json_encode($userdata);
    }
    }

this code works completly as the echo json_encode($userdata) all values are being shown in the firebug console. 此代码与echo json_encode($ userdata)完全一样,所有值都在firebug控制台中显示。 let me show you some image to understand better. 让我给你看一些图片以更好地理解。

Ajax显示空值 get_all_friends Ajax第二功能

can you please tell me where i am wrong? 你能告诉我我错了吗?

Why do you use $each to iterate over the data? 为什么使用$each遍历数据?

You can use a simple for -loop instead, so that JSON.stringify and JSON.parse can be removed 您可以使用简单的for -loop来删除JSON.stringifyJSON.parse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM