繁体   English   中英

AJAX请求成功,但返回空数据

[英]AJAX Request successful but return empty data

我在MySQL中有一个表格,并使用FOR EACH循环在页面上显示数据。 之后,当数据库中插入新的原始文件时,每次执行FOR EACH循环时,我都进行了ajax请求更新。

AJAX请求成功,但返回空数据。 我不明白我在做什么错。 我将不胜感激。 也许我做错了什么,而WordPress正在使我的代码变得奇怪?

我使用FOR EACH循环显示数据的文件: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>';

我用jquery称为ajax.php的文件:(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>

当我制作console.log(data)时,仅打印< ul id="list" >< /ul >而没有其他内容。

结果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" } }

这是我的分页:

$_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>";

说明

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM