duanliao6077 2014-10-03 07:57
浏览 499
已采纳

AJAX请求成功但返回空数据

I have a table in my MySQL and i display data on the page using FOR EACH loop. After that i did an ajax request to update every time my FOR EACH loop when in the database there is inserted a new raw.

The AJAX request is successful but returning empty data. I do not understand what I am doing wrong. I will appreciate any help. Maybe something wrong I am doing and WordPress is taking my code strange?

File That I display data with FOR EACH Loop: ajax.php

// DB Connection (Wordpress)

global $wpdb;

$_IL_START = 0;
$_IL_LIMIT = 10;

if( isset($_GET['paged']) ){
    $_IL_ID = $_GET['paged'];
    $_IL_START = ($_IL_ID-1) * $_IL_LIMIT;
}

$_IL_QUERY_INTERNAL = $wpdb->get_results( "SELECT * FROM $_IL_TABLE_NAME_INTERNAL ORDER BY il_id DESC LIMIT $_IL_START, $_IL_LIMIT");

//FOR EACH Loop

echo '<ul id="list">';
foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA)
{
    echo $_IL_RESULT_DATA->il_name;
    echo $_IL_RESULT_DATA->il_email;
    $_IL_RESULT_DATA->il_date;
}
echo '</ul>';

File That I call that ajax.php with jquery: (display.php)

//require once the FOR EACH Loop

require_once ($_SERVER["DOCUMENT_ROOT"]."/ajax.php");

//and the JS Code

<script type="text/javascript" >
    fetch();
    function fetch(){
        jQuery.ajax({
            cache: false,
            url: '/ajax.php',
            success: function(data) {
                jQuery(data).hide().prependTo("#list").slideDown("slow");
                if(jQuery("#list li").length > 15){
                    jQuery('#list li:gt(14)').remove();
                }
                console.log(data);
                setTimeout("fetch()", 100);
            }
        });
    }
</script>

When I make console.log(data) is printing this only < ul id="list" >< /ul > and nothing else.

RESULT OF var_dump($_IL_QUERY_INTERNAL);

array(3) { [0]=> object(stdClass)#427 (11) { ["il_id"]=> string(1) "6" ["il_name"]=> string(11) "Ion Luchian" ["il_email"]=> string(14) "adasdds@ffl.ll" ["il_from_mt4"]=> string(10) "2088408090" ["il_from_mt4_currency"]=> string(3) "USD" ["il_to_mt4"]=> string(6) "534534" ["il_to_mt4_currency"]=> string(3) "USD" ["il_comments"]=> string(0) "" ["il_status"]=> string(8) "approved" ["il_user_ip"]=> string(9) "127.0.0.1" ["il_date"]=> string(10) "2014-09-18" } [1]=> object(stdClass)#426 (11) { ["il_id"]=> string(1) "4" ["il_name"]=> string(11) "Ion Luchian" ["il_email"]=> string(13) "dasdas@.gg.gg" ["il_from_mt4"]=> string(10) "2088408090" ["il_from_mt4_currency"]=> string(3) "USD" ["il_to_mt4"]=> string(7) "3112312" ["il_to_mt4_currency"]=> string(3) "USD" ["il_comments"]=> string(51) "this my comment, appears only if the comment exists" ["il_status"]=> string(8) "approved" ["il_user_ip"]=> string(9) "127.0.0.1" ["il_date"]=> string(10) "2014-09-18" } [2]=> object(stdClass)#430 (11) { ["il_id"]=> string(1) "3" ["il_name"]=> string(11) "Ion Luchian" ["il_email"]=> string(14) "terterte@tt.ll" ["il_from_mt4"]=> string(10) "2088408090" ["il_from_mt4_currency"]=> string(3) "USD" ["il_to_mt4"]=> string(6) "345345" ["il_to_mt4_currency"]=> string(3) "USD" ["il_comments"]=> string(0) "" ["il_status"]=> string(8) "approved" ["il_user_ip"]=> string(9) "127.0.0.1" ["il_date"]=> string(10) "2014-09-18" } }

This is my pagination:

$_IL_ROWS_COUNT = mysql_num_rows(mysql_query("select * from $_IL_TABLE_NAME_INTERNAL"));
$_IL_TOTAL = ceil( $_IL_ROWS_COUNT / $_IL_LIMIT );

if($_IL_LIMIT < $_IL_ROWS_COUNT){
echo '<span class="il_pagination_block_admin">';
if( $_IL_ID > 1 )
{
    echo "<a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".($_IL_ID-1)."' class='il_pagination_prev'><span class='il_pagination_prev_icon'></span></a>";
}

echo "<ul class='il_pagination'>";
for( $i = 1; $i <= $_IL_TOTAL; $i++ )
{
    if( $i == $_IL_ID ) { echo "<li class='il_pagination_current'>".$i."</li>"; }

    else { echo "<li><a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".$i."'>".$i."</a></li>"; }
}
echo "</ul>";
if( $_IL_ID != $_IL_TOTAL )
{
    echo "<a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".( $_IL_ID + 1 )."' class='il_pagination_next'><span class='il_pagination_next_icon'></span></a>";
}

echo "</span>";
  • 写回答

1条回答 默认 最新

  • duangutang3940 2014-10-03 08:13
    关注

    EXPLANATION

    on the server page (php) that receive the facebook notice, you have to put an extra check so
    
    if( facebook_data_come ) { 
    
       // 1 - insert the new data inside your table
    
       // 2 - on a separate database table that you will call for example facebook_notification_check where you have an INT of 0 or 1 (default to 0), update it to 1 after the first step above.
    
       // 3 - on the ajax php page you will first check if  facebook_notification_check is equal to 1, if so you will continue with your code and reset the facebook_notification_check back to 0 otherwise you return an empty string...
    
    }
    

    <?php
    global $wpdb;
    $html = 'unknown error';
    $query = "SELECT * FROM $_IL_TABLE_NAME_INTERNAL ORDER BY il_id DESC LIMIT $_IL_START, $_IL_LIMIT";
    if($_IL_QUERY_INTERNAL = $wpdb->get_results($query)){
        if(count($_IL_QUERY_INTERNAL)){
            foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA)
            {
                $html = '<li>'. $_IL_RESULT_DATA->il_name . $_IL_RESULT_DATA->il_email . $_IL_RESULT_DATA->il_date .'</li>';
            }
        } else {
            $html = '';
        }
    } else {
        header("HTTP/1.0 500 Internal server error");
        $html = mysql_error() . " | sql query : " . $query;
    }
    echo $html;
    ?>
    

    <ul id="list"></ul>
    
    <script type="text/javascript" >
        fetch();
        function fetch(){
            jQuery.ajax({
                cache: false,
                url: '/ajax.php',
                success: function(data) {
                    console.log(data);
                    if(data!=''){
    
                        jQuery(data).prependTo("#list");
                        jQuery('#list li:first-child').slideDown("slow");
    
                        setTimeout("fetch()", 100);
                    }
                }, 
                error: function(jqXHR, textStatus, errorThrown){
                    console.log(jqXHR);
                }
            });
        }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧