sea_1987
sea_1987

Reputation: 2954

jquery ajax json help

Hello I have a PHP method which is called using $.ajax() the end result is the I return something similar to json_encode($insert), on success of the ajax call being successful I alert the return data and get the following,

{"content":"Helle this is a testasdasdasdsd","retrain":false,"created_at":1296247015,"employers_id":"4"}

I then try to narrow down the data that I show by doing this html.content however this returns as undefined when I alert, below is my code

Javascript:

$('#employer_feed').submit(function(){
    $.ajax({
        url: '/employer/feed',
        data: $('#employer_feed').serialize(),
        type: 'POST',
        success:function(html) {
            alert(html);
            $('#feed').append('<div class="feed_item">'+html.content+'</div>');
        }
    });
    return false;
});

PHP:

if($this->f->insert($insert)) {
                if(is_ajax()) {
                    echo json_encode($insert);
                }
            }

This is what the $insert looks like,

Array ( [content] => Helle this is a testasdasdasdsd [retrain] => [created_at] => 1296247448 [employers_id] => 4 )

Upvotes: 0

Views: 320

Answers (1)

Ateş G&#246;ral
Ateş G&#246;ral

Reputation: 140182

Try adding dataType: "json" to your $.ajax call. Also, you may want to set the Content-Type header to application/json from the PHP side.

Upvotes: 3

Related Questions