CodeIgniter Forums
Load View into JSON object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Load View into JSON object (/thread-24443.html)



Load View into JSON object - El Forum - 11-10-2009

[eluser]alboyd[/eluser]
**DELETE PLEASE** - I am an idiot! Smile

OMG This *must* be a stupid question - I think perhaps I am misunderstanding the purpose of JSON.

So quick background - I have a fairly complex CodeIgniter site which uses masses of JQuery AJAX calls. Currently I think my implementation of this is dodgy and so I am trying to improve. Currently I will do something like this:
Code:
..
..
$.post(base_url + "main_admin/update_user_details", { user_id_str : user_id_str },
                  function(data) {
$("div#ajax_content_bottom").html(data).fadeIn("slow");
});

This works fine. However I'll evaluate if there is an error by doing something like this:
Code:
if (data == 'Error') { do blah }

Or maybe I want to redirect for some errors or print an error message for others and it's just ugly.

So I thought I would spend some time and re-write everything using JSON returned results. But, in the example above - i'm unable to get the page content "data" returned as something like "data.content".

In my controller function I have tried this:
Code:
$content = $this->load->view('main_admin/forms/view_update_user_details_form', $this->view_data, TRUE);
            $result = array('status' => 'OK', 'content' => $content);
            echo json_encode($result);

But when loaded:
Code:
$.post(base_url + "main_admin/update_user_details", { user_id_str : user_id_str },
    function(data) {
            
    if (data.status == 'OK')
            {
        alert(data.content);
        $("div#ajax_content_bottom").html(data.content).fadeIn("slow");
        }
    }, "json");

All the dynamic elements of the view which use the contents of $this->view_data are empty. Is it not possible or not intended to put a whole output of a view into a element of a JSON object?