Q:

Can't display Ajax value

I have a problem with my ajax, it can't display data from database.
Controller
public function rating() {
 $rating = $this->db->select_avg('hasil_rating')
                    ->get('tb_rating')->row_array();
 echo json_encode($rating);
}

Ajax
function rate() {
 $.ajax({
  type: 'POST',
  url: '<?php echo base_url()."rate/rating"?>',
  dataType: 'json',
  success: function(data) {
    $('#aaaa').val(data);
  }
});

input
<input id="aaaa" type="text" value="">

when I used val() the result is [object Object] and when I used html() the result is empty. But when I use console.log(data) it works.

A:

Just convert json object to string and it will work.
$('#aaaa').val(data.someVar);

For example,
var jsonVal = {val1:'one',val2:'two'};
alert(jsonVal); // it will print [object][object]
alert(jsonVal.val1); // one
alert(jsonVal.val2); // two
alert(JSON.stringify(jsonVal)) // it will print {val1:'one',val2:'two'}

Hope it will help you.

