Php 更新验证码时,如何在AJAX中设置HTTP头img类型jpeg/png?
使用jquery+php。做自定义AJAX验证码。i、 e用户点击图像,它会自动更新Php 更新验证码时,如何在AJAX中设置HTTP头img类型jpeg/png?,php,jquery,ajax,Php,Jquery,Ajax,使用jquery+php。做自定义AJAX验证码。i、 e用户点击图像,它会自动更新 <script src="/js/jquery.js"></script> <script> $(document).ready(function(){ $.post('/captcha.php', {}, function(resp){ $("div").html(resp); }); }); </script>
<script src="/js/jquery.js"></script>
<script>
$(document).ready(function(){
$.post('/captcha.php', {}, function(resp){
$("div").html(resp);
});
});
</script>
<div></div>
$(文档).ready(函数(){
$.post('/captcha.php',{},函数(resp){
$(“div”).html(resp);
});
});
在PHP中,标头已经发送,所以如果它包含在
中,它将以jpeg格式打印captcha。问题似乎是需要发送的标题。那么,我该怎么做呢?标头是用PHP发送的。它在js中不起作用。添加到您的captcha.php文件中:
header('Content-Type: image/jpeg');
在captcha.php文件中添加:
header('Content-Type: image/jpeg');
如果要更改HTML文档中的图像,请更改图像的src属性(或用新的元素替换图像元素)。根本不要使用XMLHttpRequest。如果要更改HTML文档中的图像,请更改图像的src属性(或用新的图像元素替换图像元素)。根本不要使用XMLHttpRequest。如果您是从不同的脚本加载源,那么您只需通过字符串值发送文件的
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);
}
});
});
如果您在当前脚本中进行更改,那么不要使用ajax,而是使用后一种jQuery方法如果您从不同的脚本加载源,那么您只需通过字符串值发送文件的
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);
}
});
});
如果您在当前脚本中进行更改,则不要使用ajax,而是使用后一种jQuery方法我这样做了,如果不是ajax,它可以正常工作。我正在寻找解决方案,使这个ajaxedSo只更改jQuery的图像地址$(“imgElementId”).attr(“src”,新地址);我这样做了,如果不是AJAX的话,效果很好。我正在寻找解决方案,使这个ajaxedSo只更改jQuery的图像地址$(“imgElementId”).attr(“src”,新地址);