0

i have to get output in this format var sampleTags = ['c++', 'scala'];

My javascript function is:

 <script>
            $(document).ready(function(){
        $(function(){  
             var sampleTags;

            $.ajax({
                url:"<?php echo base_url('ajax_get_tags/gettags'); ?>"
            }).done(function(data) {
                if (data) {
                   sampleTags = data;
                }
            });
       ......................
        .......................



 $(function(){  
     var sampleTags = <?php echo json_encode($query) ?>;

My php controller is

   function gettags(){
    $json_array=$this->tagsmodel->get_all_tags(); 
   echo json_encode($json_array); 
 }

My model is

 //-------------------------------Function get all tags--------------------------------
function get_all_tags() { 
    $this->load->database();
    $this->db->limit('10');
    $this->db->select('tags_name');
    $res = $this->db->get('tags');
    $ret = array();

    foreach ($res->result_array() as $row) {
          $ret[] = $row['tags_name'];
    }

    return $ret;
}

How can get the json output from ajax request to be display its value for a javascript variable? Please help me to solve this issue..

12
  • what errors are you getting? (try checking the javascript error log) Commented May 8, 2013 at 13:23
  • @Blazemonger Actually its this is not duplicate, both are a diffrent question Commented May 8, 2013 at 13:26
  • @sgroves This error in console Uncaught TypeError: Object #<XMLHttpRequest> has no method 'done' Commented May 8, 2013 at 13:27
  • It's not at all clear to me what your new question is. "So according to you what should i do to get the correct data format as i explain it above?" is not something any of us can answer. It looks to me you should ask this as a comment on your previous question. Commented May 8, 2013 at 13:28
  • are you using an older version of jquery? try success instead of done. Commented May 8, 2013 at 13:28

2 Answers 2

0

You're using an older version of jQuery, so .done won't work. It looks like you want to add a key to your request object called complete, with the anonymous function as its value:

$.ajax({
  url: "<?php echo base_url('ajax_get_tags/gettags'); ?>",
  complete: function(data) {
      if (data) {
          sampleTags = data;
      }
  }
});

I found this out by googling your error message. One of the results was this question: Object #<XMLHttpRequest> has no method 'done'. You could have just googled the error message and figured this out yourself.

Sign up to request clarification or add additional context in comments.

7 Comments

Now i am using updated version jquery-1.9.1.min.js . Yeah i got that lesson You could have just googled the error message and figured this out yourself.
Sorry sgroves, still it is not populating the values. I checked console . i found no error .
you'll have to do some debugging then. this site isn't for others to do your debugging for you.
debugging like what? I think there is an error in the ajax funcion. Because i checked the php function is returning values
using console.log or alert to check the data variable in your JS function would be a good start. beyond that, it's up to you. no one on this site is going to teach you how to debug a program.
|
-1

I would like to give a few suggestions

  1. Check for the return value form the server side by printing it with die or exit.
  2. check the http response in firebug net tab. 3.

1 Comment

Well, if you're averse to 'hard work' you should probably give up on programming and go get a job working for the government or something. Sounds like you may be confused about the json you're getting back from your controller. Did you see this? api.jquery.com/jQuery.parseJSON

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.