如何找到挂断我的AJAX电话的内容并进行修复?

时间:2015-06-11 22:17:33

标签: javascript php jquery ajax wordpress

几天前,一个工作正常的AJAX电话突然挂在我的网站上。我不知道如何再追踪这个问题了。因为这个网站是在WordPress上,我想也许4.2.2更新搞砸了。但我又回到了旧版本,但没有修复它。

Check out the white space in the network timeline, I'm not sure what's causing this.

XHR: Timeline with XHR types only

发生了什么?当我单击一个菜单项时,它会对admin-ajax.php进行AJAX调用,然后在没有客户端活动的情况下挂起大约30秒,直到其他请求完成。我想,挂机发生在服务器上,但我不确定如何证明这一点。

这里是我的AJAX调用函数:

        function loadAjax(id, pageOrder) {
            ajaxCalled = true;
             jQuery('.side-menu').fadeIn(200, function () {
                 jQuery(this).find('li.current-menu-item').removeClass('current-menu-item');
                 jQuery(this).find('li').eq(parseInt(pageOrder)).addClass('current-menu-item');
            });
            jQuery.post(
                    ajaxurl, // link to wp-ajax.php usually generated within a theme, if not, create manually
                    {
                        action: 'get_subpages', // Action in functions.php  ajax function
                        id: id    // parameter sent
                    },
                function (response) {
                    //////console.log('response received');
                    if (response !== "0") {
                        setTimeout(function () {
                            jQuery('.ajax-loader').removeClass('loading');
                            jQuery('.ajax-loader').addClass('loaded');
                        }, 1500);
                        jQuery(".ajax-content").html(response);
                        defaultPageSetFigureHeight(windowHeight);
                        featureSidebar();
                        pullSubpages();
                        var title = jQuery('.ajax-content .content').attr('data-title');
                        jQuery('.logo .page').html(title);
                    }
                });
                jQuery(window).unbind('touchend touchstart wheel DOMMouseScroll mousewheel keyup').unbind(handlers);
            }
            function updateURL(urlPath) {
                history.pushState('data', '', urlPath);
            }
            function detectEndScroll() {
                jQuery(window).scroll(jQuery.debounce(200, function () {
                    scrollDetected = parseInt(scrollTop / windowHeight); // This needed to balance on scrolling 
                    //alert(scrollTop);
                    if (scrollAction === 'scroll') {
                        var isiPad = navigator.userAgent.match(/iPad/i) != null;
                        if (!isiPad) {
                            postionPages(); // Do not remove. Bug fix
                        }
                    }
                }));
            }
            function postionPages() {
                if (!isMobile()) {
                    scrollAction === 'disable';
                    scrollTopBeforeScrolling = scrollDetected * windowHeight;

                    //console.log("CURRENT: " + scrollTop + " BEFORE: " + scrollTopBeforeScrolling);
                    if (scrollTop > scrollTopBeforeScrolling) {
                        direction = 'down';
                    } else {
                        direction = 'up';
                    }
                    var module = scrollTop % windowHeight;
                    var diff = module / windowHeight;
                    if (diff > 0.15 && direction === 'down') {
                        scrollDetected++;
                    }
                    if (diff > 0.75 && direction === 'up') {
                        scrollDetected--;
                    }
                    //console.log("MODULE " + module + " scrollTop: " + scrollTop + " windowHeight: " + windowHeight + " Scroll: " + scrollDetected + ' DIRECTION: ' + direction);
                    if (scrollTop % windowHeight !== 0) {
                        if (!ajaxCalled) {
                            var skip = scrollDetected *  parseInt(windowHeight);
                            animateFastScrolling(skip);
                    }
                }
            }
        }

以下是get_subpages中调用的functions.php操作:

add_action("wp_ajax_get_subpages", "get_subpages");
add_action("wp_ajax_nopriv_get_subpages", "get_subpages");

function get_subpages() {
    $id = $_POST['id'];
    $post = get_post($id);
    $pageContent = $post->post_content;
    $pageTitle = $post->post_title;
    $subpageOpen = get_post_meta($id, "mbe_subpage_open", 1);
    ?>
    <div class="container content alignCenter" data-title="<?php echo $pageTitle; ?>">
        <?php
        echo apply_filters('the_content', $pageContent);
        ?>
    </div>
    <?php
    $subpages = get_post_meta($post->ID, "mbe_subpages_", 0);
    $subpageArray = array();
    foreach ($subpages as $element) {
        $element = explode("-", $element);
        $subpageIDstring = $element[1];
        array_push($subpageArray, $subpageIDstring);
    }
    $parameters = implode(',', $subpageArray);
    global $wpdb;
    $subpagesResults = $wpdb->get_results("SELECT * FROM `wp_posts` WHERE `ID` IN (" . $parameters . ") AND `post_type` LIKE 'page' ORDER BY `wp_posts`.`menu_order` DESC");
    createMapPopups();
    foreach ($subpagesResults as $subpage) {
        $subpageID = $subpage->ID;
        $isHero = get_post_meta($subpageID, 'mbe_hero', 1);
        $isMap = get_post_meta($subpageID, 'mbe_map', 1);
        $blueMapType = get_post_meta($subpageID, 'mbe_map_blue_type', 0);
        $slug = $subpage->post_name;
        $isMapHistoric = get_post_meta($subpageID, 'mbe_map_historic', 1);
        ?>
        <div class="wrapper figure subfigure <?php echo($isMap == 1 || $isMapHistoric == 1 ? "map relative notMobile" : "notmap"); ?> <?php echo ($subpageOpen == 1 ? "openStatic" : ""); ?>" id="<?php echo $slug; ?>" data-url="<?php echo get_permalink($pageID); ?>" >
            <?php
            $title = $subpage->post_title;
            $content = $subpage->post_content;
            $excerpt = get_custom_excerpt($subpage, 30, ' ... <a href="#" class="more">(Read More)</a>');
            $featured_image_url_subpage = "";

            if (has_post_thumbnail($subpageID)) {
                $image = wp_get_attachment_image_src(get_post_thumbnail_id($subpageID), 'full');
                $image_width = $image[1];
                $image_height = $image[2];
                $featured_image_url_subpage = $image[0];
                if ($isHero == 1) {
                    ?>
                    <div class="wrapper figure main" style="background-image: url(<?php echo $featured_image_url_subpage; ?>)">
                    </div>
                <?php } else if ($isMap == 1) { ?>
                    <div class="map-outer">
                        <img src="<?php echo $featured_image_url_subpage; ?>" class="img-responsive" alt="Map" />
                        <?php createMarkers($blueMapType); ?>
                    </div>
                    <?php
                } else if ($isMapHistoric == 1) {
                    ?>
                    <div class="map-outer">
                        <img src="<?php echo $featured_image_url_subpage; ?>" class="img-responsive" alt="Map" />
                <?php
                createHistoricMarkers();
                ?>
                    </div>
                        <?php
                        createHistoricMapPopups();
                    } else {
                        ?>
                    <div class="img relative top overflowHidden" style="background-image: url(<?php echo $featured_image_url_subpage; ?>)">
                    </div>
                <?php
            }
        }
        if ($isHero != 1 && $isMap != 1 && $isMapHistoric != 1) {
            ?>
                <div class="wrapper relative">
                    <header class="wrapper float absolute">
                        <div class="container">
                            <div class="header-top container">
                                <div class="no-padding col-sm-10">
                                    <div class="bar">
                                        <div class="item bar-bottom"></div>
                                        <div class="item bar-top"></div>
                                    </div>
                                </div>
                                <div class="action col-sm-2">
                                    <div class="share">(Share)
            <?php createSocialPopup($title, $excerpt, $slug) ?>
                                    </div>
                                    <div class="close"></div>
                                </div>
                            </div>
                            <h2 class="container title noafter"><?php echo $title; ?></h2>
                        </div>
                    </header>
                    <header class="wrapper normal">
                        <div class="container">
                            <div class="header-top container">
                                <div class="no-padding col-sm-10">
                                    <div class="bar">
                                        <div class="item bar-bottom"></div>
                                        <div class="item bar-top"></div>
                                    </div>
                                </div>
                                <div class="action col-sm-2">
                                    <div class="share">(Share)</div>
                                    <div class="close"></div>
                                </div>
                            </div>
                            <h2 class="container title noafter"><?php echo $title; ?></h2>
                        </div>
                    </header>
                </div>
                <div class="container content">

                    <div class="description">
                        <div class="excerpt">
            <?php echo apply_filters('the_content', $excerpt); ?>
                        </div>
                        <div class="full transition4">
            <?php echo apply_filters('the_content', $content); ?>
                            <div class="bottom baseline"></div>
                        </div>
                    </div>
                </div>
        <?php }
        ?>
        </div>
            <?php
        }

        $args = array(
            'post_type' => 'feature',
            'numberposts' => -1,
            'post_status' => 'publish'
        );
        $featuresCategory = get_post_meta($post->ID, 'mbe_feature_');
        if (!empty($featuresCategory)) {
            ?>
        <div class="wrapper figure featureWrapper overflowHidden">
            <div class="wrapper left">
            </div>
            <div class="wrapper right">
            </div>
            <div class="container">
                <div class="sidebar-holder no-padding col-sm-4 notMobile">
                    <div class="sidebar <?php echo(count($featuresCategory) > 1 ? 'double' : '' ); ?> feature-left">
        <?php
        $count = 0;
        foreach ($featuresCategory as $category) {
            $category = explode("-", $category);
            $categoryID = $category[1];
            $categoryName = $wpdb->get_results("SELECT * FROM `wp_terms` WHERE `term_id` = $categoryID");
            $categoryName = $categoryName[0]->name;
            ?>
                            <h2 class="title"><?php echo strtoupper($categoryName); ?></h2>
                            <ul>
            <?php
            $args = array('showposts' => -1, 'post_type' => 'feature', 'post_status' => 'publish', 'tax_query' => array(
                    array(
                        'taxonomy' => 'feature-category',
                        'field' => 'term_id',
                        'terms' => $categoryID)
            ));
            $features = get_posts($args);
            foreach ($features as $feature) {
                $title = $feature->post_title;
                $slug = $feature->post_name;
                ?>
                                    <li><a class="<?php echo($count === 0 ? "active" : ""); ?>" href="#<?php echo $slug ?>" data-id="<?php echo $slug ?>"><?php echo $title ?></a></li>
                                    <?php
                                    $count++;
                                }
                                ?>
                            </ul>
                            <?php } ?>
                    </div>
                </div>
                <div class="feature no-padding content col-sm-8">
        <?php
        $zindex = 0;
        foreach ($featuresCategory as $category) {
            $category = explode("-", $category);
            $categoryID = $category[1];

            $args = array('showposts' => -1, 'post_type' => 'feature', 'post_status' => 'publish', 'tax_query' => array(
                    array(
                        'taxonomy' => 'feature-category',
                        'field' => 'term_id',
                        'terms' => $categoryID)
            ));
            $features = get_posts($args);
            foreach ($features as $feature) {
                $title = $feature->post_title;
                $slug = $feature->post_name;
                $url = get_permalink($feature->ID);
                $content = $feature->post_content;
                $excerpt = $feature->post_excerpt;
                $featured_img = "";
                if (has_post_thumbnail($feature->ID)) {
                    $image = wp_get_attachment_image_src(get_post_thumbnail_id($feature->ID), 'full');
                    $image_width = $image[1];
                    $image_height = $image[2];
                    $featured_img = $image[0];
                }
                ?>
                            <div class="story" data-id="<?php echo $slug; ?>" style="z-index: 990<?php echo $zindex++; ?>">
                                <div class="relative">
                                    <header class="float notMobile">
                                        <div class="first">
                <?php if (!empty($featured_img)) { ?><img src="<?php echo $featured_img; ?>" class="img-responsive" width="186" alt="dow"/><?php } ?>
                                            <h2 class="noafter"><?php echo $title ?></h2>
                                        </div>
                                    </header>
                                    <header>
                                        <div class="container second">
                <?php if (!empty($featured_img)) { ?><img src="<?php echo $featured_img; ?>" class="img-responsive" width="186" alt="dow"/><?php } ?>
                                            <h2 class="noafter"><?php echo $title ?></h2>
                                        </div>
                                    </header>
                                </div>
                                <div class="content">
                                    <div class="excerpt"><?php echo apply_filters('the_content', $excerpt); ?></div>
                                    <div class="storyContent"><?php echo apply_filters('the_content', $content); ?></div>
                                </div>
                            </div>
            <?php } ?>
                    <?php } ?>
                </div>
            </div>
        </div>
    <?php } ?>
    <div class="wrapper">
        <div class="wrapper footer posts">
    <?php
    $menuItems = getMenuItems(2);
    for ($i = 0; $i < count($menuItems); $i++) {
        if (intval($menuItems[$i]->object_id) == $post->ID) {
            if ($i > 0) {
                $previous = $menuItems[$i - 1];
                $previous = $previous->object_id;
                $previous = get_post($previous);
            } else {
                $previous = $menuItems[count($menuItems) - 1];
                $previous = $previous->object_id;
                $previous = get_post($previous);
            }
            if ($i >= count($menuItems) - 1) {
                $next = $menuItems[0]->object_id;
                $next = get_post($next);
            } else {
                $next = $menuItems[$i + 1];
                $next = $next->object_id;
                $next = get_post($next);
            }
        }
    }
    ?>
            <div class="item col-xs-6 previous alignRight">
                <div class="button">
                    <div class="top">
                        Previous:
                    </div>
                    <div class="bottom">
                        <a href="<?php echo get_permalink($previous->ID); ?>"> <?php echo $previous->post_title; ?></a>
                    </div>
                </div>
            </div>
            <div class="item col-xs-6 next alignLeft">
                <div class="button">
                    <div class="top">
                        Next:
                    </div>
                    <div class="bottom">
                        <a href="<?php echo get_permalink($next->ID); ?>"><?php echo $next->post_title; ?></a>
                    </div>
                </div>
            </div>
        </div>
        <div class="footer download-report">
            <div class="item">
                <a href="/wp-content/uploads/2015/03/Report.pdf" target="_blank">DOWNLOAD FULL REPORT</a>
            </div>
        </div>
    </div>
    <?php
    die();
}

这可能是由内存泄漏造成的吗?如果是这样,我很难跟踪它。或者这是在PHP的某个地方发生的?

这可能是显而易见的,但我承认,我不太清楚自己在做什么。我最初自己并没有编写所有这些代码,但我现在负责该网站。让我知道,如果有更多信息,我可以为您提供所有信息。感谢。

UPDATE 6/12/15:我们发现当我们将WordPress版本还原为4.1.5时,AJAX过程正常工作 - 这必定意味着最近的一个WordPress更新打破了它,是吗?

0 个答案:

没有答案