Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am stuck at some json parsing, i am populating some fields using jquery's ajax function, i found the desired result, but dont know how to print it, here is my code

function logged_in_users() {
    $.ajax({
        url: site_url+'user/get_loggedin_users',
        dataType : 'json',
        success: function(result) {
            $('#div_id').append(result.email_address)
        }
    }); 
}

while i have a result in my console response like,

[{"email_address":"[email protected]"},{"email_address":"[email protected]"}]

while on the function side i am using,

echo json_encode($this->login_history_model->get_logged_in_users());

Any help will greatly be appreciated

share|improve this question

1 Answer

up vote 1 down vote accepted

You've got an array of results, not just one result, so your success function won't work.

Change to:

success: function(result) {
    $.each(result, function(i, v) {
        // For each record in the returned array
        $('#div_id').append(v.email_address); 
    });
}
share|improve this answer
 
Was trying to post similar answer, you code faster than me. –  Barmar May 7 at 13:41
 
Haha, early bird catches the worm and all that. –  Chris Dixon May 7 at 13:41
 
@Chris Dixon, simply Superb! –  Irfan Ahmed May 7 at 13:46
 
You're welcome ^^ –  Chris Dixon May 7 at 13:48
 
@Barmer, Thank you for your attention, please contribute a little to show a gif image while ajax function is fetching records, Thanks in advance –  Irfan Ahmed May 7 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.