1

I've to write an long poll query from my database - currently using mysqli. I'm successfully calling the data and return in my javascript code, but, now is the trick part, after 2 minutes, I receive this error: "Uncaught SyntaxError: Unexpected end of input".

I googled and read a couple pages (more then 30 I guess), but I wasn't able to solve this one... everyone says is most likely that I forgot to close some brackets... but I guess not..

In other hand I prefer use json.parse() than eval().. and neither of them is working...

Thank for the attention.
Ps.: I'm not english native speacker, sorry any misspelling;)


That is my current js file

var old_msg_id = "<?php echo $old_msg_id; ?>";

function waitForMsg() {
    $.ajax({
        type: "GET",
        url: "poll.php?old_msg_id=" + old_msg_id,
        async: true,
        cache: false,
        //dataType : 'json',

        success: function (dataRespond) {
            //var jsonAnswer = eval("(" + dataRespond + ")");
            var jsonAnswer = JSON.parse(dataRespond);
            if (jsonAnswer.msg !== "") {
                alert("New msg added to base!");
                console.log(jsonAnswer.msg);
            };
            old_msg_id = jsonAnswer.old_msg_id;
            setTimeout('waitForMsg()', 1000);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Deu merda!: Error: " + textStatus + " (" + errorThrown + ")");
            setTimeout('waitForMsg()', 15000);
        }
    });
}
$(document).ready(function () {
    waitForMsg();
});

That is my poll.php

error_reporting(0);
$conn = mysql_connect("localhost", "root", "");
mysql_select_db('padova', $conn) or die('Could not select database.');
$result = mysql_query("SELECT id FROM test ORDER BY id DESC LIMIT 1");
if($result === FALSE) {
    die(mysql_error());
}
$old_msg_id = $_GET['old_msg_id']; 
$result = mysql_query("SELECT id, text FROM test ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
    $last_msg_id = $row['id'];
    $msg = $row['text'];
}
while($last_msg_id <= $old_msg_id)
{
    usleep(1000);
    clearstatcache();
    $result = mysql_query("SELECT id, text FROM test ORDER BY id DESC LIMIT 1");
    while($row = mysql_fetch_array($result))
    {
        $last_msg_id = $row['id'];
        $msg = $row['text'];
    }
}
$response = array();
$response['msg'] = $msg;
$response['old_msg_id'] = $last_msg_id;
$response = array_map('htmlentities',$response);
echo json_encode($response);
Fred Dutra
  • 11
  • 1
  • Welcome to Stack Overflow! I cut away your long introduction, as it's not needed and considered "noise" here. Take a look at the [Site Tour](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask) guide. Your first question is not catastrophic, but it's in general important to read those. – Xan Sep 30 '14 at 12:31
  • What does this question have to do with Chrome Extensions, though? – Xan Sep 30 '14 at 12:34
  • My bad, wrong tag :) Sorry. – Fred Dutra Sep 30 '14 at 12:39
  • Did you look at what is being returned from the server, my guess is it is not proper JSON. – epascarello Sep 30 '14 at 12:44
  • Sorry, but how can I do that? When I look to my poll.php, I receive this: `{"msg":"Just more one test...","old_msg_id":"88"}`. And that is valid, right? – Fred Dutra Sep 30 '14 at 12:54
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 30 '14 at 13:17
  • Jay Blanchard, I do use MYSQLI in my real code. But this one is for test only. If you look closer you will see that if poor designed. I have no intention to let him this way. :) – Fred Dutra Sep 30 '14 at 13:23

0 Answers0