我正在尝试对一个从目录输出图像的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来调用它时,没有任何反应。
答案 0 :(得分:1)
您可以尝试使用$('#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);
?>
答案 1 :(得分:0)
试试这段代码:
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');
答案 2 :(得分:0)
$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);
答案 3 :(得分:0)
将断点放在来自服务器[你的php脚本]的数据中并检查它,它是否以正确的格式出现。