Skip to content Skip to sidebar Skip to footer

How To Pass Data From Controller To Jquery (ajax) In Codeigniter

controller code $rates['poor'] = 10; $rates['fair'] = 20; $this->load->view('search_result2', $rates); //I have tried this in many ways but at least It executes the 'succe

Solution 1:

  1. Uncomment dataType:'JSON'
  2. Set output as json with echo or set_output from controller
  3. Get item with rates.poor or rates['poor'] from ajax

Controller

publicfunctionpost_url()
{
    $rates = array();
    $rates['poor'] = 10; 
    $rates['fair'] = 20;

    $this->output->set_output(json_encode($rates));
}

Ajax

<script>
$.ajax({
    type:'POST',
    url:'POST_URL',
    data:{'adID':adID},
    dataType:'JSON',
    success:function(rates){ 
        $('#rate_val').html('<label>'+rates.poor+'</label>');
    }
});
</script>

Solution 2:

$data['a']='100';
$data['b']='200';
echo json_encode(array('success'=>$data));

jquery

<script>
$.ajax({
    type:'POST',
    url:'POST_URL',
    data:{'adID':adID},
    dataType:'JSON',
    success:function(rates){
       var data     = jQuery.parseJSON('['+response+']');

        $('#rate_val').html('<label>'+data.succcess.a+'</label><label>'+data.succcess.b+'</label>');
    }
});
</script>

i believe it may execute as you want to .

Post a Comment for "How To Pass Data From Controller To Jquery (ajax) In Codeigniter"