使用Ajax重新加载验证码验证码、加载、Ajax

2023-09-10 16:31:50 作者:米西米西

这问题可能会被要求已经 - 但不幸的是,我无法找到任何令人满意的答案。我只要求它为我的具体情况,并要求管理员不要删除的问题,至少数天,所以我可以尝试一下......

This question may have been asked already - but unfortunately, I could not find any satisfactory answers. I will just ask it for my concrete case and ask the admins not to delete the question for at least a few days so I can try it out...

我有一个页面。它采用了验证码。像这样:

I have a page. It uses a captcha. Like so:

<?php
session_start(); // the captcha saves the md5 into the session
?>
<img src="captcha.php" onclick="this.src = this.src" />

这是我第一次code。它没有工作,因为浏览器condsidered无用到,如果来源是相同的重载的图像。我目前的解决方案是通过GET参数:

That was my first code. It did not work, because the browser condsidered it useless to reload an image if the source is the same. My current solution is to pass a get parameter:

onclick="this.src = 'captcha.php?randomNumber='+ranNum"

JavaScript变量 VAR ranNum 是随机每次的onclick 事件触发产生。它工作得很好,不过,我不喜欢的可能性,如果 - 虽然不可能 - 的情况下两个数相同时,连续两次。虽然约50,000至50,000之间的随机数发生变化 - 我还是不喜欢它。而且我不认为该方法是正确的。我想知道的'点对点'的方法,通过AJAX的方式。我知道这是可能的。我希望你知道它是如何可能的^^在这种情况下,请告诉我。

The JavaScript variable var ranNum is generated randomly every time the onclick event fires. It works fine, still, I don't like the possibility, if the - though improbable - case of two numbers being the same twice in a row. Although the random number varies between -50,000 and 50,000 - I still do not like it. And I don't think the method is right. I would like to know the 'righter' method, by means of AJAX. I know it's possible. I hope you know how it's possible ^^ In that case, please show me.

在此先感谢!

顺便说一句 - 如果我拼帽(T)茶不同,没关系,引用到PHP文件是正确的,我的code:我用randomImage.php

By the way - if I spell cap(t)cha differently, never mind, the reference to the PHP file is right in my code: I use randomImage.php

编辑:在JavaScript中的随机数只产生这样的形象重新加载。 Captcha.php不关心$ _GET参数。该字符串确实是随机的。

The random number in JavaScript is only generated so the image reloads. Captcha.php does not care for the $_GET parameter. The string really is random.

编辑:所以,我想知道的是如何让浏览器relaod图像,而无需每次传递不同的GET参数的事件触发

Thus, what I would like to know is how to make the browser relaod the image without passing different get parameters every time the event fires.

推荐答案

不幸的是,AJAX并没有提供一个很好的方式来动态加载图像。即使是使用JavaScriptpreLOAD招只是让浏览器载入每个图像每一次URL。唯一的好办法让浏览器从同一个源中加载另一幅图像是使用不同的参数,就像你现在正在做。而且,像其他的答案已经指出,时间戳应该是足够的了。

Unfortunately, AJAX doesn't provide a good way to dynamically load images. Even using the javascript "preload" trick just gets the browser to load each image once per URL. The only good way to get the browser to load another image from the same source is to use a different parameter just like you are doing now. And, like other answers have stated, timestamp should be sufficient for that.