Ray
Ray

Reputation: 884

Simple jQuery AJAX JSON Call

I am attempting to post user data to a php script and return the results to be displayed in an element. All data is being sent the using json method..

Everything is fine up until the response. When trying to append the response to an element i get undefined, however a simple console log shows the correct results. The php side of the house is fine, data is being returned with a json encoded array.

Here is my jquery.

$('#roll').click(function(){
     $.ajax({ url: 'roll.php',
             data: {'region' : '46'},
             dataType: 'json',
             type: 'post',
             success: function(data){
                $('#result').append('<span>' + data + '</span>');
                console.log(data);
            }
    });

    return false;

})

PHP

$data = array('roll' => $roll, 'status' => $status);
echo json_encode($data);

Side note: Although I am proficient with javascript & jquery, I haven't really ever had the need to work with ajax before, so this is my first so called attempt at it. If there is a better way to do things, please let me know.

Upvotes: 0

Views: 619

Answers (1)

zgood
zgood

Reputation: 12581

Try this:

$('#result').append('<span>' + data.roll + '</span>');

Upvotes: 1

Related Questions