CodeIgniter Forums
Get data from controller by AJAX - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Get data from controller by AJAX (/showthread.php?tid=62641)



Get data from controller by AJAX - vertisan - 08-09-2015

Hi!

I tried get data from controller by AJAX but I wrote something wrong.
Here is my code:

CONTROLLER:
PHP Code:
$data['AcceptedServices'] = $this->AdminServices_model->GetAcceptedServicesAdmin(); // I get by result()

echo json_encode($data);
$this->load->view'admin/ServicesList'$data ); 

VIEW:
PHP Code:
    $(document).ready(function() {
            $(
'#DawajDane').click(function() {
                $.
ajax({
                    
url'<?php echo base_url(); ?>admin/AdminMain/GetServices',
                    
type'POST',
                    
dataType'json',
                    
success: function ( data) {
                        
console.log(data);
                    },
                    
error: function ( data ) {
                        
console.log('error');
                    }
                });
            });
        }); 

I want display this data first in console, but in console i recive 'error'.
Where is the problem?


RE: Get data from controller by AJAX - phpilyo - 08-09-2015

remove $this->load->view( 'admin/ServicesList', $data );


RE: Get data from controller by AJAX - CroNiX - 08-09-2015

You are returning 2 things, first the json and then an html view file. Jquery doesn't know what to do with that because it expects 'json'

Try something like:
PHP Code:
//If the request was made via ajax, return the json and exit. Else return the view. So when going to the page, the view will be retrieved via normal get request, and the ajax request would return the json at the same time.
if ($this->input->is_ajax_request())
{
  echo 
json_encode($data);
  exit;
}
$this->load->view'admin/ServicesList'$data ); 

Another approach would be to return both the view and the json as part of the json.

PHP Code:
$data['AcceptedServices'] = $this->AdminServices_model->GetAcceptedServicesAdmin(); // I get by result()
$data['html'] = $this->load->view'admin/ServicesList'$dataTRUE ); //3rd parameter TRUE to return view instead of immediately outputtig it
echo json_encode($data); 

Then in your ajax request, you will have the 'AcceptedServices' and 'html' that you can do with what you want.


Code:
$('#DawajDane').click(function() {
      $.ajax({
          url: '<?php echo base_url(); ?>admin/AdminMain/GetServices',
          type: 'POST',
          dataType: 'json',
          success: function ( data) {
               $('#some-div').html(data.html); //output the returned html into #some-div
               console.log(data.AcceptedServices); //view the returned json in the console
          },
          error: function ( data ) {
              console.log('error');
          }
      });
});



RE: Get data from controller by AJAX - Blair2004 - 08-10-2015

Hi,
maybe the returned response is not in json format. Check XHR request from Google Dev Tools.