0

I am using a remote API to pull an application's console logs every 2 seconds using AJAX. This works fine in all cases with the exception of the user clicking a link on the page to move elsewhere. Doing so triggers the Error callback without actually providing any data, although refreshing the page does not.

Can anyone perhaps tell me what could be causing this and a potential solution to the problem?

<script>
var server_id = getUrlParameter('id');
var lastFetchTime = 1;

$(document).ready(function()
{
    getConsoleMessages();

    setInterval(function()
    {
        getConsoleMessages();
    },2000);
});

function getConsoleMessages()
{
    var consoleBox = document.getElementById("console");

    $.ajax({
        type: "POST",
        url: "consoleHandler.php",
        data: { 'action': 'getConsoleMessages', 'server_id': server_id, 'since': lastFetchTime },
        dataType: 'json',
        success: function(data)
        {
            console.log(data);

            var messages = data['message']['chatdata'];

            if (messages.length > 0)
            {
                messages.forEach( function (item)
                {
                    // Skip lines we've already displayed
                    if (lastFetchTime >= item.timestamp)
                    {
                        return;
                    }

                    var timestamp = item.time; // /Date(1440823073243)/
                    var timestamp = timestamp.substring(timestamp.lastIndexOf("(")+1, timestamp.lastIndexOf(")"));

                    var contents = item.message;
                    lastFetchTime = item.timestamp;

                    // Get Date/Time in Milliseconds
                    var date = new Date(parseInt(timestamp));
                    var time = pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());

                    console_log("<font color='blue'>[" + time + "]</font>" + " " + contents);
                });
            }

            consoleBox.scrollTop = consoleBox.scrollHeight;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert("Status: " + textStatus); alert("Error: " + errorThrown); alert("Message: " + XMLHttpRequest.responseText);
        }
    });
};
</script>

consoleHandler.php

$action = $_POST['action'];
$response = array();

switch ($action)
{
    case "getConsoleMessages":
    {
        foreach ($_SESSION['servers'] as $server)
        {
            if ($server['whmcs_id'] == $_POST['server_id'])
            {
                $mcmyadmin = new McMyAdmin($server['mcma_user'], $server['mcma_pass'], $server['mcma_ip'], $server['mcma_port']);

                $result = $mcmyadmin->getChat($_POST['since']);

                $response['status'] = "success";
                $response['message'] = $result;
            }
        }

        echo json_encode($response);

        break;
    }
}
  • the user moves to other page after clicking the link? – geekbro Nov 23 '16 at 11:39
  • Yeah, everything works as expected, the alert boxes are displayed with empty data shown, then after clicking through them, it proceeds to the page that they clicked. Commenting out the alert box allows it to work as normal but I don't want to leave an unusual error unresolved. –  Nov 23 '16 at 11:46
  • how can be assured that whenever the user clicks the function **getConsoleMessages()** is fired?.coz i can see that functional is called after every 2 seconds and not on any event – geekbro Nov 23 '16 at 11:52
  • It seems that the page waits for the next getConsoleMessages() interval to run before it proceeds to move on to the next page. –  Nov 23 '16 at 11:54
  • May be the three events i.e moving to next page,alert,and ajax call all happens in last 2 seconds .the page moves to new page and at the same time ajax is also fired which leads to return error. – geekbro Nov 23 '16 at 12:02

0 Answers0