[英]jquery ajax Long Polling append error parsererror (SyntaxError: Unexpected token <)
I want to make a ticker like facebook with Jquery Ajax long poll and php. 我想用Jquery Ajax long poll和php做一个类似于facebook的代码。
My script display: error parsererror (SyntaxError: Unexpected token <). 我的脚本显示:error parsererror(语法错误:意外的标记<)。
What is the cause of these error in my script please? 请问我的脚本中这些错误的原因是什么?
My Jquery script: 我的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 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
your problem maybe this: 您的问题可能是这样的:
echo json_encode($data); 回声json_encode($ data);
I also had this problem and i'm using this: 我也有这个问题,我正在使用:
echo $_GET['callback'] . echo $ _GET ['callback']。 '(' . "{answer : true}" . ')';
'('。“ {answer:true}”。')';
Where callback is just a parameter in your request query. 回调只是请求查询中的一个参数。 Also try dataType:'jsonp'
也尝试dataType:'jsonp'
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.