weixin_33743703 2014-12-23 12:33 采纳率: 0%
浏览 102

Ajax结果显示未定义

I want to ues ajax polling for show user update by followers like facebook.

So I collect this code and apply in my page, Which append only 'undefined' one after one.

What is my wrong here in my code please.

At below I give my full polling script and related file

my Table name: updateside

id - work_id - parent_id - from_id - to_id - sub - detail - img - created
..........................................................................

AI - work_id, parent_id etc. all data submit by user post form

My JavaScript

function waitForMsg(){

    $.ajax({
        type: "GET",
        url: "upsidenew.php",
        async: true, 
        cache: false, 
        timeout:50000, 
        success: function(data){ 
            if(data) {
               $("#updatetime").append('<div class="upbox1">' + data.detail + '</div>');
            }
            setTimeout(
                waitForMsg, 
                1000 
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(
                waitForMsg, 
                15000);
        }
    });
}

$(document).ready(function () {
    waitForMsg(); 
});

upsidenew.php

$parent = //collect from other query
date_default_timezone_set('Asia/Dhaka');
$timestamp = date("M j, y; g:i a", time() - 2592000);

$u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' AND `created` > '".$timestamp."' ORDER BY created DESC") or die(mysqli_error($dbh));
$response = array();
while ($row = mysqli_fetch_array($u)) {
    $response['from_id'] = $row['from_id'];
    $response['parent_id'] = $row['parent_id'];
    $response['to_id'] = $row['to_id'];
    $response['sub'] = $row['sub'];
    $response['detail'] = $row['detail'];
    $response['img'] = $row['img'];
    $response['time'] = $row['created'];

    ?><script><?php echo '(Content-Type: application/json)';?></script><?php
    echo json_encode($response);
    exit;
}
  • 写回答

3条回答 默认 最新

  • weixin_33720956 2014-12-23 12:37
    关注

    Add your ajax request dataType as "dataType: 'json'"

    $.ajax({
            type: "GET",
            url: "upsidenew.php",
            async: true, 
            cache: false, 
            timeout:50000, 
            dataType: 'json'
            success: function(data){ 
            if(data) {
               $("#updatetime").append('<div class="upbox1">' + data.detail + '</div>');
            }
                setTimeout(
                    waitForMsg, 
                    1000 
                );
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                addmsg("error", textStatus + " (" + errorThrown + ")");
                setTimeout(
                    waitForMsg, 
                    15000);
            }
        });
    
    评论

报告相同问题?