-1

I have encountered a really weird situation. Im developing a software, to which I want to add a feature to confirm email, so basically, it sends an ajax request to my server and server responds with a key, this key will be also sent in the email within a link. This key is saved as a variable and then I add a setInterval, which runs a link, which by the key responds if the user has clicked the link or not. The data is stored in mysql. That everything works perfectly fine with one little issue. The AJAX responses do not update and just keep the old value although when I open the checking link in my browser the response is indeed different, does anybody have any thoughts what might by causing this issue?

        $.ajax({
            url: "http://...&em=" + email,
            type: "GET",
            success: function(rese){
                if(rese === "E1") {
                }else{
                    var checker = setInterval(function(){
                        $.ajax({
                            url: "http://...&key=" + rese,
                            type: "GET",
                            success: function(resee){
                                alert(resee);
                            }
                        });
                    }, 3000);
                }
            }
        });

Edit - Server responses: so, the data is updated in the moment, when user does click the link, the value changes to "yes", while normally, it would respond with just "no", but anyway, ajax keeps alerting me with no, even when i just have updated the value straight in the database

IDNK
  • 21
  • 4
  • Show us what triggers this ajax and where you define `email` in the process – charlietfl Jul 18 '20 at 20:01
  • @charlietfl the ajax is just a part of if statement if(setupPassword != confirmPassword) {...}else{ ... var email = $("#setupEmail").val(); - here is the ajax - } – IDNK Jul 18 '20 at 20:15
  • Also, the 2ND ajax inside the first one is problematic, the first one is absolutely ok – IDNK Jul 18 '20 at 20:16
  • Fine but there simply aren't enough debugging details given. In particular there is nothing shown that would give an indication that server would send different response or what expected response is and what you are receiving – charlietfl Jul 18 '20 at 20:22
  • oh okay, wait for the edit, Ill add how the server does respond – IDNK Jul 18 '20 at 20:31
  • That interval is going to continue using the value returned from first ajax until you stop it using `clearInterval()` When you make another request with a new email... the original interval continues making old value requests and you also start a new interval timer making newer value requests. Not sure why you even need that setInterval – charlietfl Jul 18 '20 at 20:37
  • @charlietfl Well, its because I want it to be actually look like some sort of a cool dynamic software, that just will automatically check if you clicked the link, so the user doesnt have to input numbers or something of that nature – IDNK Jul 18 '20 at 20:43
  • Also, do you have any suggestions, how to actually work a way around this? Without intervals im pretty well done – IDNK Jul 18 '20 at 20:45
  • What is the reason for the interval in the first place? – charlietfl Jul 18 '20 at 20:48
  • It just checks if the value is "yes", if it is it would just alert it, thats true, but also, if it would actually work, I would further add a code with different actions, just literalelly its like some sort of a "listener?" – IDNK Jul 18 '20 at 20:57
  • OK but need to understand that it is done in a closure and will need to clear old one before starting a new one with revised `rese` value. Or move it out of the closure – charlietfl Jul 18 '20 at 21:15
  • Actually, rese doesnt amplify the result of the second ajax, so there is a different issue. As originally mentioned, it just doesnt update at all, so I would need to reset the variable, I tried to add remove in every loop, but it seems to have no effect. I also moved the ajax out of the original ajax, but there was no luck at all – IDNK Jul 18 '20 at 21:24
  • Well you probably geta different `rese` when email is updated...but...the original interval is still working with the old `rese` – charlietfl Jul 18 '20 at 21:39
  • Am I able to do somehow find a way around this issue? – IDNK Jul 18 '20 at 21:42
  • Make sure to use clearInterval before starting a new one with updated email ajax – charlietfl Jul 18 '20 at 21:43
  • @charliefl I also tried a custom way to alternate interval, now it seems that there is actual problem with jquery, but I will try that too – IDNK Jul 18 '20 at 21:54

1 Answers1

-1

try adding a dynamic url to prevent cache:

              $.ajax({
                url: "http://...&em=" + email,
                type: "GET",
                cache: false,
                success: function (rese) {
                    if (rese === "E1") {
                    } else {
                        var checker = setInterval(function () {
                            $.ajax({
                                url: "http://...&key=" + rese,
                                type: "GET",
                                cache: false,
                                success: function (resee) {
                                    alert(resee);
                                }
                            });
                        }, 3000);
                    }
                }
            });
toto
  • 1,180
  • 2
  • 13
  • 30