wordpress ajax keeps losing connections

I apologize for my being little more han a newbie in WP and ajax but I need your help. I’m not sure if it’s a bug or it’s simply me who cannot manage to find a solution.

I’ve the following situation.

Read More

I’ve a plugin that displays certain data from a remote mysql server. This data need to be updated periodically.

At the moment I’ve implemented this through AJAX, more or less, like this:

$.ajax({
    type : "POST",
    url: cktn_ajax_object.ajax_url,
    data: params,
    dataType: "json",
        success: function(response) {
        << Update  UI according to response >>
    },
    error: function(request, status, err) {
        console.debug("[AjaxOnTimer Error] status: " + status);
    }
});

where $.ajax is calling the relevant php script responsible for retrieving the remote mysql data. The PHP script duration is variable, according to the amount of data it fetches (Let’s say, something between a few seconds and four or five minutes).

If I test the whole on my local machine, there is no problem. The ajax call duly and patiently awaits the (slow) response of the local PHP server (which is querying the remote mysql server) and eventually, upon completion, it updates my UI.

On the other hand, when I install my plugin on the production machine things don’t work as expected. Just a few seconds after having placed my ajax call to the PHP server, I get the following error in the console of the browser:

Failed to load resource: connection lost. —> admin-ajax.php

and my script fails leaving me no other clue than the word error in the ‘status’ parameter of the ajax call.

Apparently the heartbeat.lock-post is interfering with my ajax call.
Any idea of what am I doing wrong?

Related posts

2 comments

  1. Add the following to functions.php to turn off heartbeat:

    add_action( 'init', 'my_deregister_heartbeat', 1 );
    function my_deregister_heartbeat() {
        global $pagenow;
    
        if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )
            wp_deregister_script('heartbeat');
    }
    

    And restart the apache.

Comments are closed.