jquery ajax Long Polling追加错误parsererror(SyntaxError:Unexpected token<)

时间:2014-12-24 10:54:22

标签: javascript jquery ajax

我想用Jquery Ajax长轮询和php制作像facebook这样的自动收报机。

我的脚本显示:error parsererror(SyntaxError:Unexpected token<)。

请问我的脚本中出现这些错误的原因是什么?

我的Jquery脚本:

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

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

upsidenew2.php

include_once("mysessionone.php");
if($session->logged_in){
global $dbh;
$myid = $session->id;

//Find out follow And followers
$q = mysqli_query($dbh,"SELECT * FROM follow WHERE friend_one='$myid' OR     friend_two='$myid'") or die(mysqli_error($dbh));
if ($row = mysqli_fetch_array($q)) {
$f_id = $row['friend_one'];
$f2_id = $row['friend_two'];

//convert follow and followers id's
$s1 = mysqli_query($dbh,"SELECT bdid,username FROM users WHERE `bdid`='".$f_id."' OR `bdid`='".$f2_id."'") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($s1)) {
$fbdid = $row["bdid"];
$fusername = $row['username'];

//find out post id's of user's and followers
$g = mysqli_query($dbh,"SELECT parent_id FROM updateside WHERE `from_id`='".$fusername."' OR `to_id`='".$fbdid."' OR `from_id`='".$session->username."' OR `to_id`='".$session->id."' GROUP BY parent_id DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($g)) {
$parent = $row['parent_id'];
date_default_timezone_set('Asia/Dhaka');
$timestamp = date("M j, y; g:i a", time() - 2592000);

//here is user's related post to echo
$u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' AND `created` > '".$timestamp."' ORDER BY created DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($u)) {
$data['from_id'] = $row['from_id'];
$data['parent_id'] = $row['parent_id'];
$data['to_id'] = $row['to_id'];
$data['sub'] = $row['sub'];
$data['detail'] = $row['detail'];
$data['img'] = $row['img'];
$data['time'] = $row['created'];

header('Content-type: application/json');
echo json_encode($data);
exit;

}
}
break;
}
} else { echo '<div class="mass1">No Update</div>'; }
} else { echo '<div class="mass1">Login to see</div>'; }            //session close

1 个答案:

答案 0 :(得分:0)

你的问题可能就是这样:

echo json_encode($ data);

我也遇到了这个问题,我正在使用它:

echo $ _GET [&#39; callback&#39;]。 &#39;(&#39;。&#34; {answer:true}&#34;。&#39;)&#39;;

回调只是请求查询中的参数。还可以尝试dataType:&#39; jsonp&#39;

希望这有帮助。