【问题标题】:AJAX Request Not Getting Any InfoAJAX 请求未获得任何信息
【发布时间】:2012-05-03 22:30:09
【问题描述】:

这是我的 PHP 文件代码:

<?php
require('config.inc'); //Init DB.

$current_user_id = $_GET['uid']; //Set User ID From AJAX GET.

mysql_query("USE social-network"); //Set MySQL DB.

$userFavs = "SELECT subscribee_id FROM subscribe WHERE subscriber_id = '" . $current_user_id . "'"; //Get people user is subscribed to.

$query="SELECT * FROM posts WHERE (post_owner_id IN ($userFavs) OR post_owner_id = '" . $current_user_id . "') ORDER BY id DESC"; //Select posts by user AND by people user is subscribed to.

$result=mysql_query($query); //Do the query.

$num=mysql_numrows($result); //Get number of rows in query.

$i=0; //Display selected posts.
while ($i < $num) {

$owner_id=mysql_result($result,$i,"post_owner_id");
$content=mysql_result($result,$i,"content");
$date=mysql_result($result,$i,"date");


$poi_query=mysql_query("SELECT firstname, lastname, profile_picture FROM `social-network`.`users` WHERE id = '" . $owner_id . "'") or die(mysql_error());
$post_firstname=mysql_result($poi_query, 0, "firstname");
$post_lastname=mysql_result($poi_query, 0, "lastname");
$post_profile_picture=mysql_result($poi_query, 0, "profile_picture");
?>

      <div class="post">
        <h1 class="post-title"><a href="profile.php?user=<?php echo $owner_id; ?>"><?php echo $post_firstname; ?> <?php echo $post_lastname; ?></a></h1>
        <p class="content"><?php echo $content; ?></p>
        <p class="details"><?php echo $date; ?></p>

      </div>

      <?php
$i++;
}
?>

这是我的 JS AJAX 请求:

function loadPosts()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","PHP/news-feed.php?uid=<?=$current_user_id?>",true);
xmlhttp.send();
document.getElementById("posts").innerHTML=xmlhttp.responseText;
}

我页面上的帖子部分下没有显示任何内容。我究竟做错了什么?并且 PHP 代码经过测试,当我直接将其包含在帖子页面中时它就可以工作。 我正在尝试做的事情:PHP Newsfeed with Reload

【问题讨论】:

  • 使用调试器来查看发生了什么,可能是为 firefox 安装 firebug 扩展并在其控制台中查看问题出在哪里,是发送 ajax 请求,是服务器失败还是什么

标签: php javascript ajax


【解决方案1】:

id 为 "posts" 的元素在哪里?,我只能看到一个带有 post 类的 div 标签

【讨论】:

    【解决方案2】:

    您的 ajax 有一个问题,它假设是同步的:您期望在发送请求后立即得到结果,但您应该在 onreadystatechange 事件上附加一个事件处理程序。

    如果您对 ajax 没有任何经验,我建议您使用像 jQuery 这样的库来为您处理。

    除此之外,我也看不到您在哪里调用 ajax 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多