Welcome Guest, Not a member yet? Register   Sign In
Load View into JSON object

**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:
$.post(base_url + "main_admin/update_user_details", { user_id_str : user_id_str },
                  function(data) {

This works fine. However I'll evaluate if there is an error by doing something like this:
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:
$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:
$.post(base_url + "main_admin/update_user_details", { user_id_str : user_id_str },
    function(data) {
    if (data.status == 'OK')
    }, "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?

Theme © iAndrew 2016 - Forum software by © MyBB