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

[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?


Messages In This Thread
Load View into JSON object - by El Forum - 11-10-2009, 07:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB