Skip to content
Advertisement

loading a view after an ajax call, CodeIgniter

I have an ajax call from javascript to a method in a controller, the method in the controller should be loading a view(like, show a new page on the screen), Although everything seems ok, the new view doesn’t seem to load on the screen. I cannot use header or windows.location because, i am passing an array variable, containing data to be used in the view.

The page is visible under the Network tab (preview sub tab, while selecting the ajax) of the Chrome debugging console. But the main screen stays as it is.

If somebody has faced a similar issue, or has a solution, please help me.

Thanks!!

Advertisement

Answer

Ok here is what you are doing wrong.

when you request the page using ajax it does not return you that page.

When you use $this->load->view(‘pagename’,$datapassed); it loads the view and that’s why you see nothing.

What you have to do is use

$data=$this->load->view('pagename',$datapassed, TRUE);

What it will do is, it will return that page and save it in $data after that you can print it using

$this->set_output($data); 

and receive this in ajax result and load it in a div.

and if you want to refresh the whole page you can use

$(body).html(result);

You have to understand that you need to send the html page by supplying that third parameter in load view.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement