![]() |
How to return a view to an ajax call ? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: How to return a view to an ajax call ? (/showthread.php?tid=1285) |
How to return a view to an ajax call ? - Tao Pai Pai - 02-25-2015 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 ![]() RE: How to return a view to an ajax call ? - silentium - 02-25-2015 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/general/views.html RE: How to return a view to an ajax call ? - Tao Pai Pai - 02-26-2015 @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. RE: How to return a view to an ajax call ? - james - 02-26-2015 If you want to get a result in json, you can do so: Code: $data = $this->upload->data(); RE: How to return a view to an ajax call ? - spjonez - 03-01-2015 Store it using a key so it's valid JSON: Code: $result = array( |