我正在尝试对从目录输出图像的php脚本进行ajax调用。
我有这个作为我的php脚本:
<?php
$con = @mysqli_connect('myhost','myusername','myuserpass','myuserDB') or die('Could not connect to the database.'." ". __FILE__ ." ". __LINE__);
$photoFile = mysqli_query($con, 'select photoFile from photoInfo');
while ($p = mysqli_fetch_array($photoFile)){
echo '<img class="image" src='."../img/".$p['photoFile'].".jpeg />";
};
mysqli_close($con);
?>
我试着这样称呼它。
$(document).ready(function() {
$.ajax({
url: 'php/getImages.php',
method: 'get',
dataType: 'html'
}).done(function(data){
$('#imageBox').appendData(data);
});
});
我到底做错了什么?当我自己运行php脚本时,它会显示图像,但当我尝试使用ajax调用它时,什么都没有发生。
你能试试这个吗,用$('#imageBox').html(data);
而不是$('#imageBox').appendData(data);
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: 'php/getImages.php',
method: 'get',
dataType: 'html'
}).done(function(data){
$('#imageBox').html(data);
});
});
</script>
<div id="imageBox"></div>
在getImages.php中,
<?php
$con = @mysqli_connect('myhost','myusername','myuserpass','myuserDB') or die('Could not connect to the database.'." ". __FILE__ ." ". __LINE__);
$photoFile = mysqli_query($con, 'select photoFile from photoInfo');
while ($p = mysqli_fetch_array($photoFile)){
echo '<img class="image" src="../img/'.$p['photoFile'].'.jpeg" />';
};
mysqli_close($con);
?>
试试这个代码:
while ($p = mysqli_fetch_array($photoFile)){
$img_arr[]= '<img class="image" src='."../img/".$p['photoFile'].".jpeg />";
};
echo json_encode($img_arr);
exit;
在javascript 中
$.get('php/getImages.php',function(data)
$.each(data,function(k,e){
html+=e;
});
$('#imageBox').appendData(html);
},'json');
$photoFile = mysqli_query($con, 'select photoFile from photoInfo');
while ($p = mysqli_fetch_array($photoFile)){
$imgpath = "../img/".$p['photoFile'].".jpeg";
echo '<img class="image" src="'.$imgpath.'" />';
};
mysqli_close($con);
将断点放在来自服务器[您的php脚本]的数据中,并检查它的格式是否正确。