user3650099
user3650099

Reputation: 131

Ajax.stop function with json request issue

I am trying to read from a database some data but without waiting for the page to load, so I created an ajax post that starts sending data when page is ready, now after ajax completes I need to read the values from another file. The problem is that after the ajax completes, json that is reading the data is running indefinite.

JQUERY

<?php $url = $_GET['url']; ?>
var jQuery_1_11_0 = $.noConflict(true);
jQuery_1_11_0(document).ready(function () {
    var domain = '<?php echo $url; ?>';
    $.ajax({
        type: 'POST',
        url: 'lib/ajax.php',
        data: {
            action: 'get_all_seo_details', // function that collects data
            domain: domain // the domain that is being send to the function in order to get data
        },
        success: function (data) {
        // doesn't need to echo anything only to insert the data, which it done properly
        }
    });
  // Below is the second part of the script that starts when ajax stops
    $(document).ajaxStop(function () {
        $.getJSON('lib/get-details.php', function(data) {
            var twitter_followers = data.twitter_followers;
            $('#twitter-followers').html(twitter_followers);
        });
 // data is being read correctly but it loops repeatedly in the console without finishing
    });
});

PHP - get-details.php, reading the data from database after getting inserted with ajax

if (!isset($_SESSION)) {
    sec_start();
}
global $db;
$domain = isset($_SESSION['domain']) ? $_SESSION['domain'] : '';
if ($domain == '') {
    $domain = $db->query("SELECT * FROM seo_data");
} else {
    $domain = $db->query("SELECT * FROM seo_data WHERE domain = '$domain'");
}
$domain_now = $domain->fetch_assoc();
$twitter_followers = (int) $domain_now['twitter_followers'];
echo json_encode(array('twitter_followers' => $twitter_followers));

Upvotes: 0

Views: 233

Answers (1)

syl.fabre
syl.fabre

Reputation: 746

I'm not sure but when the first AJAX request stops

$(document).ajaxStop(function (){....

starts a new one with

$.getJSON('lib/get-details.php', function(data) { ...

When this second one ends, maybe

$(document).ajaxStop(function (){....

is called again which starts again the 2nd request and so on

Upvotes: 1

Related Questions