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);
            }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝