Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax结果显示未定义_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript Ajax结果显示未定义

Javascript Ajax结果显示未定义,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想使用ajax轮询来显示facebook等关注者的用户更新 所以我收集这些代码并在我的页面中应用,页面只一个接一个地追加“未定义” 请问我的代码有什么问题 在下面,我给出了完整的轮询脚本和相关文件 我的表名:updateside id - work_id - parent_id - from_id - to_id - sub - detail - img - created ............................................................

我想使用ajax轮询来显示facebook等关注者的用户更新

所以我收集这些代码并在我的页面中应用,页面只一个接一个地追加“未定义”

请问我的代码有什么问题

在下面,我给出了完整的轮询脚本和相关文件

我的表名: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
我的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;
}

将ajax请求数据类型添加为数据类型:“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);
        }
    });

如果未将ajax调用的数据类型显式设置为json,则需要使用以下命令解析结果:

jsondata = $.parseJSON(data);
alert(jsondata.detail);
如中所示


如果返回的是JSON,那么除了echo JSON_encode$response之外,不应该输出任何内容。这一行:

?><script><?php echo '(Content-Type: application/json)';?></script><?php

你为什么要退出环路?这样循环只执行一次。若你们只想返回一行,为什么你们有一个循环?删除了它,但结果是一样的。删除了什么?我在回答中说要删除的行?删除$response=array;您不应该删除它。请尝试从浏览器中打开upsidenew.php。在json编码字符串完成之前,确保没有空格等。你能从你的文件中发送一个显示chrome网络的图片链接吗?这个文件使用ajax请求。如果你收到一个已经发送的消息头,你需要修复这个问题。你不应该在这行之前输出任何东西。请确保在输入之前没有空行
header('Content-type: application/json');