我知道这个问题有点孩子气,但我无法找到解决这个问题的正确方法......
我正在使用jquery和ajax调用网站中的用户搜索功能,php返回json对象......
当我使用php文件搜索用户时,如果json返回只有一个数组,jquery会在屏幕上打印它,但是当返回多个结果时,我不知道要打印出来....
这是从php返回的结果:
{"search":"10
junaid
saleem
junaid@yahoo.com
"}{"search":"13
zzz
aaa
zzz@yahoo.com
"}
这里是jquery网页:
<?php
session_start();
require("secure_scripts/getusers.php");
require("secure_scripts/getdp.php");
require("secure_scripts/getusersinfo.php");
if(!isset($_SESSION['id'])){
header("location: index.php");
}else{
$zxcv_lgn = base64_encode($_SESSION['id']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome <?php echo getusers("first_name"); ?> | Addressbook.com</title>
<script src="jquery.js" type="text/javascript" ></script>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
$(document).ready(function(){
$("#search_button").click(function(){
$("#search_button").click(function(){ $("#console_loader").hide(); });
$("#console_loader").fadeIn("slow").html("<img src='images/ajax-loader.gif' id='ajax-loader' />");
var send = $("#search").val();
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function(msg){
$("#ajax-loader").fadeOut("slow", function(){
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(msg.search);
});
}
});
});
});
</script>
</head>
<body>
<div id="header">
<p><a href="index.php"><img src="images/header_logo.png" /><span>AddressBook™</span></a></p>
</div>
<div id="wrapper" align="center">
<div id="main">
<div id="content">
<div id="top_nav">
<div class="userinfo"><span class="user_title">Welcome <?php echo getusers("first_name")." ".getusers("last_name"); ?></span></div>
<div class="search">
<form onsubmit="return false" id="search_form">
<input type="text" name="search" class="search_box" id="search" placeholder="Type in to search...">
<input type="button" id="search_button" class="sea" name="search_submit"value="search">
</form>
</div>
</div>
<div id="left_nav">
<div id="dp"><img src="<?php echo getdp(); ?>"></div>
<div class="left_nav_links">Profile</div>
<div class="left_nav_links">Contacts</div>
<div class="left_nav_links">Settings</div>
<div class="left_nav_links">privacy</div>
<div class="left_nav_links">logout</div>
</div>
<div id="console">
<div id="console_top_nav">Your Contacts:</div>
<div id="console_content">
<div id="console_loader" style="display: none;"></div>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="links"><ul><li><a href="index.php">Home</a></li><li><a href="about">About</a></li><li><a href="contact">Contact</a></li></ul></div>
<div id="copyrights">© 2014 Addressbook.com All Rights Reserved</div>
</div>
</div>
</body>
</html>
只有一个对象从php返回,如:
{"search":"13
zzz
aaa
zzz@yahoo.com
"}
它完美无缺,但没有多个json对象......
提前感谢!
答案 0 :(得分:1)
您需要使用jQuery的.each()
方法,如下所示:
$(document).ready(function(){
$("#search_button").click(function(){
$("#search_button").click(function(){ $("#console_loader").hide(); });
$("#console_loader").fadeIn("slow").html("<img src='images/ajax-loader.gif' id='ajax-loader' />");
var send = $("#search").val();
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function (msg) {
$.each(function (index, item) {
$("#ajax-loader").fadeOut("slow", function () {
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(item.search);
});
});
}
});
});
});
当收到你的json时,它很可能是一个对象数组
[{"search":"10
junaid
saleem
junaid@yahoo.com
"}{"search":"13
zzz
aaa
zzz@yahoo.com
"}]
因此,通过使用$.each()
遍历集合并返回值(index, item)
,您可以通过引用它来获取对象的值,如下所示:
$("#console_loader").fadeIn("slow").html(item.search);
因为json正在返回一个JavaScript对象文字。
答案 1 :(得分:0)
这样的事情应该有效:
$.ajax({
type: "POST",
url: "secure_scripts/search_users.php",
data: {search: send},
dataType: "json",
success: function(msg){
$.each(function() {
$("#ajax-loader").fadeOut("slow", function(){
$("#console_loader img").remove();
$("#console_loader").fadeIn("slow").html(msg.search);
});
});
}
});
我们正在向成功函数添加$.each()
方法,以便为返回的每个JSON对象运行它,而不仅仅是第一个。
Here's the jQuery.each() doc进一步阅读。
为了清晰起见而编辑