Viktors
Viktors

Reputation: 935

Get problems returning html data from json

I have got jquery post function

jQuery('.more-button').live('click', function(eve){

    var page = jQuery(this).attr('id').replace('more-button-','');

    loaded_messages += 1;

    jQuery.post('http://127.0.0.1/auth_system_1/user_activity/users_picture_number', { loaded_messages : loaded_messages }, function(data) {

                    jQuery('#ajax_content').append(data.html);


            }, 'json');

});

My php function return json (I use Codeigniter):

function users_picture_number()
{

        $per_page = (int)$this->input->post('loaded_messages');

        $user_id = 3;

        $data['images_list'] = $this->user_activity_lib->users_pictures($user_id, $per_page);

        $html = $this->load->view('front_end/ajax/ajax_users_pictures', $data);

        echo json_encode(array('html' => $html));
}

json return to response html code but in top is shown this line {"html":null}

it return me : enter image description here

if I replace this line $html = $this->load->view('front_end/ajax/ajax_users_pictures', $data); with $html = $this->load->view('front_end/ajax/ajax_users_pictures', $data, true); it return

enter image description here

I try to show html which return json in jQuery('#ajax_content')

How to solve this problem ?

Upvotes: 0

Views: 834

Answers (3)

Daniele
Daniele

Reputation: 1081

I had a similiar problem, turns out I was printing some critical characters (accented characters) without using the htmlentities function or similiar.

Upvotes: 0

user800014
user800014

Reputation:

To store the html of a view in a var, the correct is indeed the second way:

$html = $this->load->view('front_end/ajax/ajax_users_pictures', $data, true);

For test you can change the method to return html and echo the content of $html.

Upvotes: 1

AgnosticMantis
AgnosticMantis

Reputation: 716

You are trying to send JSON and normal HTML. In PHP your $html variable is empty. Check the function that is returning your value. And you have to choose if you want to transmit JSON or HTML. You set your content type to json within your ajax call, so please stick to it and dont send HTML too.

Upvotes: 0

Related Questions