admin管理员组文章数量:1414932
Here is my captcha.php script: (it creates an image)
// creating a image in this script and set its value on the session
Also I have a <img>
in the contact_us.php file for showing that captcha image:
<img src="http://localhost/captcha.php" class="captcha_pic" alt="Captcha">
Also I have an ajax code for submiting that form in the contact_us.js (this file is atteched to contact_us.php):
$("#f_contact_form").on("submit", function (e) {
e.preventDefault(e);
$.ajax({
url: $("#f_contact_form").attr("action"),
type: 'POST',
data: $("#f_contact_form").serialize(),
dataType : 'JSON',
success: function (contact) {
$(".contact_message").html(contact.message);
// here I need to reload that image url for changing the captcha image
}
});
});
It should be noted that when I change the url of that image using [inspect element] (in the static) and then I write the correct name, the image will be changed. for example I replace the current image url with src=".../captcha.ph"
and then I write the correct name src=".../captcha.php"
, then the page will be changed (exactly what I want).
Now there is any solution for reloading captcha image?
Here is my captcha.php script: (it creates an image)
// creating a image in this script and set its value on the session
Also I have a <img>
in the contact_us.php file for showing that captcha image:
<img src="http://localhost/captcha.php" class="captcha_pic" alt="Captcha">
Also I have an ajax code for submiting that form in the contact_us.js (this file is atteched to contact_us.php):
$("#f_contact_form").on("submit", function (e) {
e.preventDefault(e);
$.ajax({
url: $("#f_contact_form").attr("action"),
type: 'POST',
data: $("#f_contact_form").serialize(),
dataType : 'JSON',
success: function (contact) {
$(".contact_message").html(contact.message);
// here I need to reload that image url for changing the captcha image
}
});
});
It should be noted that when I change the url of that image using [inspect element] (in the static) and then I write the correct name, the image will be changed. for example I replace the current image url with src=".../captcha.ph"
and then I write the correct name src=".../captcha.php"
, then the page will be changed (exactly what I want).
Now there is any solution for reloading captcha image?
Share Improve this question edited Oct 3, 2015 at 16:40 Shafizadeh asked Oct 3, 2015 at 15:06 ShafizadehShafizadeh 10.4k15 gold badges58 silver badges92 bronze badges 2- 1 Try and see if any cache buster works(ex: appending time as query string) – user405398 Commented Oct 3, 2015 at 15:11
-
can't inject image file contents directly into the dom , image must be in
<img>
tag. Look into how to use base64 encoded image url – charlietfl Commented Oct 3, 2015 at 15:23
3 Answers
Reset to default 2You need to reload your image with a unique query string attached to it, in order to prevent your browser from caching the image. The easiest way is to just append the current time. Since you are already using jQuery, here's one way you can do it.
var unique = $.now();
$('.captcha_pic').attr('src', 'http://localhost/captcha.php?' + unique);
However, some people may remend against this solution as it can fill up your cache. Although the alternative will require that you send cache control headers.
There's a lot of solution for there. Here are some of your options.
Remove your images , then create a new one.
$("#images").remove(); $("div").html("<img src='http://localhost/captcha.php' class='captcha_pic' alt='Captcha'>")
Using unique to avoid cache.
$("#f_contact_form").on("submit", function (e) { e.preventDefault(e); $.ajax({ url: $("#f_contact_form").attr("action"), type: 'POST', data: $("#f_contact_form").serialize(), dataType : 'JSON', success: function (contact) { $(".contact_message").html(contact.message); $('.captcha_pic').attr('src', 'http://localhost/captcha.php?' + new Date().getTime()); } }); });
$.now() has been deprecated in jQuery 3.3. Correct way based on accepted answer:
var unique = Date.now();
$('.captcha_pic').attr('src', 'http://localhost/captcha.php?' + unique);
本文标签: javascriptHow to reload an image using ajaxStack Overflow
版权声明:本文标题:javascript - How to reload an image using ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745171455a2646000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论