I am having a problem in fetching data from database using jQuery AJAX as json datatype I have read and tried JSON.parse in jQuery but it did not work. All I am returning in console.log(data)
is object
and the data from database in object but I don't know how to show it on view. Any suggestions please.
Here is my Controller.
function fetch(){
$data_fetch = array(
'id' => $this->input->post('txt_id')
);
$data['records'] = $this->form_model->fetch_model($data_fetch);
$results = json_encode($data);
$this->output->set_content_type('application/json');
$this->output->set_output($results);
}
Here is my jQuery AJAX
$.ajax({
type : 'POST',
url : base_url + 'index.php/form_controller/fetch',
data : {txt_id : id },
dataType : 'JSON',
success : function(data){
// var re = JSON.parse(data);
// var re = $.parseJSON(data);
// var data = JSON.parse(data);
alert(data.name);
console.log(data);
},
error : function(data){
console.log('erronr in fetch');
}
});
The data.name
is showing undefined
in alert. Any help will be appreciated.