2014-12-24 50 views
0

我想使用JQuery Ajax長民意調查和PHP做一個像facebook一樣的股票代碼。jquery ajax長輪詢附加錯誤parsererror(SyntaxError:意外的標記<)

我的腳本顯示: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 
+0

你能通過瀏覽器以某種方式檢查你的AJAX請求的響應數據嗎? (可能是網絡檢查員)我認爲這個問題可能在你的PHP腳本的響應中。 –

+0

網絡巡視員顯示ok響應數據 – joy

+0

這不是我的意思。響應數據是否以'<?開頭? –

回答

0

您的問題也許是:

回聲json_encode($的數據);

我也有這個問題,我使用這個:

回聲$ _GET [ '回調']。 '('。「{answer:true}」。')';

其中callback只是您的請求查詢中的一個參數。另外嘗試dataType:'jsonp'

希望這會有所幫助。

+0

我是否替換echo $ _GET ['callback']。 '('。「{answer:true}」。')';與回聲json_encode($數據);? dataType:'jsonp'不起作用。 – joy

+0

控制檯用這個「設置」說什麼? –

+0

同樣的錯誤顯示 – joy

相關問題