简体   繁体   English

AJAX请求未获取任何信息

[英]AJAX Request Not Getting Any Info

Here's my PHP file code: 这是我的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++;
}
?>

Here's my JS AJAX request: 这是我的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;
}

Nothing shows up under the posts section on my page. 我页面上的帖子部分下没有任何显示。 What am I doing wrong? 我究竟做错了什么? And the PHP code is tested and it worked when I just directly included it in the posts page. 当我直接将其直接包含在帖子页面中时,PHP代码已经过测试并且可以正常工作。 What I'm trying to do: PHP Newsfeed with Reload 我正在尝试做的: 带重新加载的PHP Newsfeed

ID为“ posts”的元素在哪里?,我只能看到带有post class的div标签

One thing that is wrong with your ajax, is that it is assuming to be synchronous: You are expecting a result right after you send your request but instead you should attach an event handler on the onreadystatechange event. ajax的一件事是错误的,它假定是同步的:发送请求后,您期望得到一个结果,但是应该在onreadystatechange事件上附加事件处理程序。

If you don't have any experience with ajax, I would recommend using a library like jQuery to handle that for you. 如果您对ajax没有任何经验,我建议您使用jQuery之类的库来为您处理。

Apart from that I don't see where you are calling your ajax function either. 除此之外,我也看不到您在哪里调用ajax函数。

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

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