Javascript 使用ajax从单个页面加载多个数据
我有下面的代码从php页面获得http响应,这很好,但是我想知道是否可以在该php页面上有另一个查询,并在主页上显示该数据。我是否应该为我提出的每个请求都提供一页Javascript 使用ajax从单个页面加载多个数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有下面的代码从php页面获得http响应,这很好,但是我想知道是否可以在该php页面上有另一个查询,并在主页上显示该数据。我是否应该为我提出的每个请求都提供一页 $(document).ready(function() { loadData(); }); var loadData = function() { $.ajax({ //create an ajax request to load_page.php type: "GET", u
$(document).ready(function() {
loadData();
});
var loadData = function() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "second.php",
dataType: "html", //expect html to be returned
success: function(response){
$(".display").html(response);
setTimeout(loadData, 1000);
}
});
};
second.php
//first query
$inbox = $msg->prepare("SELECT * FROM messages where to_user = ? and deleted != ? and del2 != ? and is_read = '' order by id desc");
echo $msg->error;
$inbox->bind_param('sss', $username->username, $username->username, $username->username );
$inbox->execute();
$unread = $inbox->get_result();
//second query
$stmt2 = $msg->prepare("");
$stmt2->bind_param('', $var....
html
如果要同时在同一位置显示这两个请求的结果(在您的示例中:$(“.display”).html(响应);
),可以在一个响应中输出它们。更少的ajax请求。同样的结果。@Tseho不在同一个地方。所以,这是两种不同的反应。您需要有两个ajax请求。您可以使用两个不同的php文件,但也可以尝试以下操作:url:“second.php?data=users”、
和url:“second.php?data=messages”、
和:if(isset($\u GET[“data”])&$\u GET[“data”=“users”){//first request}if(isset($\u GET[“data”])&$\u GET[“data”=“messages”){//second request}
<div class="display"></div><!--The response is displayed here-->
<div></div><!--I want the the result of second query to be displayed here-->