0

I Have created a generic function to handle three simple ajax requests to backend code. I only have one single problem left, and that is if systemName "AD" returns a code 0, then the two following functions should not be fired at all.. But it looks like it runs the function and directly after tests if the condition sADsuccess=1 or not, and dosent wait for the function to set the variable.. So my question is: How can i write this in a better way?

 var sADsuccess=1;            
        $("#frmPwdChg").submit(function(e) {
            var formzData =  $("#frmPwdChg").serialize();

            ChangeSystemPwd('UPDATE/AD/USER/PASSWORD/',formzData,'AD',1,0,0)
                if(sADsuccess=1){
                 ChangeSystemPwd('UPDATE/Cosmic/PASSWORD/',formzData,'Cosmic',0,0,0);
                 ChangeSystemPwd('UPDATE/Imprivata/PASSWORD/',formzData,'Imprivata',0,1,1);
                }
                else{
                    $("#btnPwdChg").removeClass("loading");
                    $("#btnPwdRes").addClass("hidden");
                    $("#btnLogout").removeClass("hidden");
                    $("#frmPwdChg")[0].reset();
                }
            e.preventDefault(); // avoid to execute the actual submit of the form.
        });            


function ChangeSystemPwd(APIurl,sFormData,sSystemName,loadStart,loadEnd,bEmptyForm){
var url = sUrl + APIurl

$("#btnPwdChg").addClass("loading"); 
$.getJSON({
       type: "POST",
       url: url,
       data: sFormData,
       success: function(response) //Om Ajax anropet lyckas
       {
           var sMessage=response[0].message;
           var sCode=response[0].code

            if(sCode===1) {
                showGreen(sSystemName,sMessage)
                sessionStorage.setItem(sSystemName, 'success');
        }
            else if(sCode===0) {
                showRed(sSystemName,sMessage);
                sessionStorage.setItem(sSystemName, 'failure');
                if(sSystemName='AD'){
                    alert('AD')
                    sADsuccess=0;
                }
        }  
            else if(sCode===-1){
                //anropet kom tillbaka som att det inte fanns någon användare. Visa ingenting
                sessionStorage.setItem(sSystemName, 'no-user');
        }
       },
       error: function(xhr,response){ //Om ajax anropet inte lyckas, hämta status och skriv ut ett meddelande
        showErr(xhr,response,'#errResponse')
        },
       complete: function(xhr,data){
        if (loadEnd==1){
            $("#btnPwdChg").removeClass("loading"),$("#btnPwdRes").addClass("hidden"),$("#btnLogout").removeClass("hidden");      
                }
        if (bEmptyForm==1){
            $("#frmPwdChg")[0].reset();
        }        
        }

});

}

assembler
  • 3,098
  • 12
  • 43
  • 84
Wyrm
  • 25
  • 1
  • 5
  • 1
    Use promises and resolve with the result code. [Changing a global variable asynchronously does not work](https://stackoverflow.com/q/23667086/1048572). – Bergi Dec 06 '17 at 19:37
  • `if(sADsuccess=1){` will always be true, you're *assigning* instead of *comparing*. Additionally, you're mixing synchronous and asynchronous operations. – David Dec 06 '17 at 19:39
  • thanks. i realize that i have a lot of things still to learn. thx for the pointers.. i will continue to see if i can wrap my head around this... – Wyrm Dec 07 '17 at 01:31

1 Answers1

0
sADsuccess=1

is an assignment, not a comparison. Instead of =, use == or ===.

Niklas Higi
  • 2,188
  • 1
  • 14
  • 30