1

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 ajax call. However I am getting only the following response in my Chrome debugger and nothing happens.

10
  • 1
    In your ajax call what value you are getting for base_url ?? Commented May 9, 2015 at 6:46
  • /switchLang/changeLang is repeating in above url. Is it the correct URL ?? Commented May 9, 2015 at 6:51
  • This is what I meant: localhost:8080/a1/switchLang/changeLang Commented May 9, 2015 at 7:05
  • Seems like I always have problem with the url in codeigniter. But in this case, I don't think my url is incorrect. Because I try copy and pasting the url in my browser and it is working perfectly Commented May 9, 2015 at 7:07
  • 1
    Try without reloading as alredy suggested. Since you are not posting anything to controller, for sake of test try with AJAX GET type to see what will be result. Commented May 9, 2015 at 12:38

1 Answer 1

1

i think there may be quotation problem change:

var base_url = '<?php echo site_url('switchLang/changeLang');?>';

to

var base_url = '<?php echo site_url("switchLang/changeLang");?>';

Sign up to request clarification or add additional context in comments.

2 Comments

What is the difference between those two lines? j
what is shown in console log by type console.log(data); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.