Thread: Json

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2

    Json


    hello! i have a json code here. how do you avoid returning undefined in json? I am using codeigniter.

    here is my view.

    PHP Code:
    <script>
                                                    function 
    changeLoad(o) {
                                                      var 
    op o.value;
                                                      
    //var op = $("#category").val(); 
                                                      
    $.ajax({
                                                        
    url"pms/casecat",
                                                        
    type"POST",
                                                        
    asyncfalse,
                                                        
                                                        
    data: {
                                                            
                                                            
    keywordop
                                                        
    },
                                                        
    dataType"json"
                                                      
    }).done(function(data){
                                                        var 
    caseName = $("#casename")[0];
                                                        
    alert(data.result);
                                                        for(var 
    i=0i<data.lengthi++){
                                                        var 
    option document.createElement("option");
                                                        
    option.setAttribute("value"data.value);
                                                        
    option.appendChild(document.createTextNode(data.value));
                                                        
    caseName.appendChild(option);
                                                        }
                                                      });
                                                    }
                                                
    </script> 
    here is my controller.

    PHP Code:
    function casecat(){
                
    $keyword $this->input->post('keyword');
                
    $this->load->model('Case_model');
                
    $cases $this->Case_model->get_Case_by_category($keyword);
                echo 
    json_encode($cases);
            } 
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2
    thanks again for the reply. when i try to alert(data.value) it pops undefined.
  6. #4
  7. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2
    ahm, values are specifically strings. Child Abuse/ Abandonment, Physical Abuse (PA), Sexual Abuse (SA) etc. that should be the ones returned.
  10. #6
  11. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    One more time: What is the exact value of data? I don't mean what you expect to see in it, I don't mean the values it should have, I don't mean data.value, I don't mean anything except for the exact contents of the data variable.

    You can find out using code like
    Code:
    alert(data);
    Code:
    console.log(data);
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2
    when I try alert(data) it pops with the content of

    <!DOCTYPE HTML>

    <html>

    <head>


    and cant scrool it down. =(
  14. #8
  15. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    That doesn't look like JSON data. Guess there's some problem between your Javascript and your PHP that's affecting what gets executed...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2
    using my previous code. I just added
    PHP Code:
    <?php echo $base?>index.php
    in my url in ajax.

    now when I alert(data) this returns [{"casename": "Name of cases"}]

    but when I alert(data.value) it is undefined.????
  18. #10
  19. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    That's right, because data is an array (not an object) containing an object with a .casename (not a .value). That's the next thing to troubleshoot.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    2
    I got it! I solved it when I saw something over the internet.

    I converted the data being returned in JSON by using

    var json = JSON.parse(data);

    And also I can now get each column by using json[i].casename!!!

    Thank you requinix for helping!! peace! =)

IMN logo majestic logo threadwatch logo seochat tools logo