Имеется код:
main.php
<?php
if(isset($_POST['getNewId'])){
loadNewPosts();
}
function loadNewPosts(){
include 'db-config.php';
$new = $_POST['getNewId'];
$newId = mysql_fetch_array(mysql_query("SELECT * FROM posts ORDER BY id DESC"));
if($newId>$new){
if($new==0){
$post1 = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 10");
}else{
$post1 = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT $new , $newId");
}
$post = mysql_fetch_array($post1);
do{
print(json_encode(array('post'=>'<div class="area"><div class="pad-5">'.$post['content'].'</div></div>','id'=>$newId)));
}while($post=mysql_fetch_array($post1));
}else{
print(json_encode(array('id'=>$new)));
}
}
main.js
setInterval(loadNewPosts,5000);
var newId = 0;
function loadNewPosts(){
$.ajax({
url:SiteURL+'assets/includes/functions/main.php',
type:'POST',
data:{'getNewId':newId},
success:function(text){
text = JSON.parse(text);
if(text!=''){
$('.postContainer').prepend(text.post);
newId = text.id;
}else{
alert(text);
}
}
});
}
После выполнения кода воводится ошибка в консоли JavaScript:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse ()
А вот и
json ответ от php:
<!--error--><br />
<b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>Z:\home\mind\www\assets\includes\functions\main.php</b> on line <b>17</b><br />
<script language=JavaScript src='/denwer/errors/phperror_js.php'></script>{"post":"<div class=\"area\"><div class=\"pad-5\"><\/div><\/div>","id":{"0":"48","id":"48","1":"8","poster_id":"8","2":"\u041a\u0430\u043a \u0443 \u0432\u0430\u0441 \u0434\u0435\u043b\u0430?","content":"\u041a\u0430\u043a \u0443 \u0432\u0430\u0441 \u0434\u0435\u043b\u0430?"}}<!--error--><br />
<b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>Z:\home\mind\www\assets\includes\functions\main.php</b> on line <b>20</b><br />
<script language=JavaScript src='/denwer/errors/phperror_js.php'></script>