带有json请求问题的Ajax.stop函数

时间:2014-07-17 11:32:17

标签: javascript php jquery ajax json

我试图从数据库中读取一些数据,但没有等待页面加载,所以我创建了一个ajax帖子,当页面准备好后开始发送数据,现在在ajax完成后我需要从另一个文件中读取值。问题是在ajax完成之后,正在读取数据的json无限期地运行。

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,在插入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));

1 个答案:

答案 0 :(得分:1)

我不确定,但第一个AJAX请求何时停止

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

开始一个新的
$.getJSON('lib/get-details.php', function(data) { ...

当第二个结束时,也许

$(document).ajaxStop(function (){....
再次调用

,再次启动第二个请求,依此类推