vendredi 8 mai 2015

CodeIgniter - Ajax Json call to controller not working

I am current using CodeIgniter 2.2.2 I have the following controller code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class switchLang extends CI_Controller {
    public function __construct()
    {
        parent::__construct();

                $this->load->helper('url');
    }
    public function changeLang()
    {
            log_message('debug', 'INSIDE');
            //echo "test";

            $data = array("STATUS"=>"true");
            //echo "HI";
            echo json_encode($data) ;   
    }

    public function index(){
        }   
}

And inside my view I have the following ajax call:

<script  type="text/javascript">
            $(document).ready(function() {      
                                var base_url = '<?php echo site_url('switchLang/changeLang');?>';

                                $('#lang').click(function(event) {
                                        console.log(5 + 6);
                                        $.ajax({
                    'async': false,
                    'url' : base_url,
                    'type' : 'POST', 
                    'dataType': 'json',
                    'data' : 'data',
                    'success' : function(data){ 
                                console.log(data);
                                 if(data){
                                    location.reload(); 
                                }
                             }
                                    });

                                });
                        });
</script>

I am trying to reload page on success jax call. However I am getting only the following response in my Chrome debugger and nothing happens.

enter image description here

What exactly is mock post and how come my ajax isn't calling my controller function? Note: But when I try manually typing url inside my browser((myhostname)/switchLang/changeLang), my controller seems to be working perfectly.

Aucun commentaire:

Enregistrer un commentaire