[英]AJAX (jquery) to run PHP script not working
I am trying to make an ajax call to a php script that outputs images from a directory. 我正在尝试对从目录输出图像的php脚本进行ajax调用。
I have this as my php script: 我将其作为我的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);
?>
And I'm trying to call it like this. 我正试图这样称呼它。
$(document).ready(function() {
$.ajax({
url: 'php/getImages.php',
method: 'get',
dataType: 'html'
}).done(function(data){
$('#imageBox').appendData(data);
});
});
What all am I doing wrong? 我到底在做什么错? When I run the php script on its own, it brings up the images but when I try to use ajax to call it, nothing happens. 当我自己运行php脚本时,它会弹出图像,但是当我尝试使用ajax调用它时,什么也没有发生。
Can you try this, use $('#imageBox').html(data);
您可以尝试使用$('#imageBox').html(data);
instead of $('#imageBox').appendData(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>
In getImages.php, 在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);
?>
Try this code: 试试这个代码:
while ($p = mysqli_fetch_array($photoFile)){
$img_arr[]= '<img class="image" src='."../img/".$p['photoFile'].".jpeg />";
};
echo json_encode($img_arr);
exit;
In javascript 在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脚本]并检查它,是否采用正确的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.