How to return a view to an ajax call ? |
Hi,
I recently had to modify some code of my colleague. I saw that we don't handle ajax calls the same way. When it's about returning data, we both use. Code: echo json_encode($this->upload->data()); However, when we have to return a whole view, we are a bit different : He uses : Code: echo $this->load->view('theview',$data); I use : Code: $view = $this->load->view('theview',$data, TRUE); I also saw somewhere online Code: $data = $this->load->view('theview',$data, TRUE); Those techniques seem to work correctly, but what the best pratices say about that ? Is one of them safer, faster or more "MVC friendly" ? Thanks
I would just load the view as normal, following the CI guidelines. That would be the simplest and fastest solution.
Code: $this->load->view('my_view', $data); No need to have echo in front, or place it in an variable and echo later. More info about views: http://www.codeigniter.com/userguide3/ge...views.html
@silentium, Thanks for answering. Actually it's a bad copy/paste, there was indeed no echo in his part.
I've already read the doc, and there is no recommendation for that particular case. Probably because there is no need to I guess. I will read the core function that load views to see if that third parameter can spare me some execution time.
If you want to get a result in json, you can do so:
Code: $data = $this->upload->data(); |
Welcome Guest, Not a member yet? Register Sign In |