使用jquery + php。自定义AJAX验证码。也就是说,用户点击图片,它会自动更新。
<script src="/js/jquery.js"></script>
<script>
$(document).ready(function(){
$.post('/captcha.php', {}, function(resp){
$("div").html(resp);
});
});
</script>
<div></div>在PHP头中已经发送,所以如果它包含到<img src="/captcha.php" />中,它将打印jpeg格式的验证码。问题似乎是需要发送的报头。那么,我该怎么做呢?标头是用PHP发送的。它在js中不起作用。
发布于 2012-04-11 18:40:23
如果要更改HTML文档中的图像,请更改图像的src属性(或用新的图像元素替换图像元素)。完全不要使用XMLHttpRequest。
发布于 2012-04-11 18:37:46
在captcha.php文件中添加header function:
header('Content-Type: image/jpeg');发布于 2012-04-11 18:50:47
如果您从不同的脚本加载源,则只需通过字符串值发送文件的src。这样,您的captcha.php代码就会变成如下所示
$source="/path/to/the/file.jpg";
header("Content-type:text/plain");
print $source;收到后,您可以执行以下操作来更改源
$("#changeCaptchaButton").click(function() {
$.ajax({
url: "captcha.php",
cache: false,
success: function(data) {
alert("changing source of image");
var source=data;
$("#captchaImg").attr("src", source);
}
});
});如果要在current script中进行更改,请不要使用ajax,而应使用后一种jQuery方法
https://stackoverflow.com/questions/10104289
复制相似问题