Welcome Guest, Not a member yet? Register   Sign In
Get data from controller by AJAX
#1

(This post was last modified: 08-09-2015, 07:28 AM by vertisan.)

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?
Reply
#2

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

(This post was last modified: 08-09-2015, 09:48 AM by CroNiX.)

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');
          }
      });
});
Reply
#4

(This post was last modified: 08-10-2015, 12:29 AM by Blair2004.)

Hi,
maybe the returned response is not in json format. Check XHR request from Google Dev Tools.
NexoPOS 2.6.2 available on CodeCanyon.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB