Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var rcode = 0;

//微博认证函数
        function CNY_CheckWeiDo() {            
            

            $.post("do_chy_anthorize.aspx", function (data) {
                var result = eval('(' + data + ')');
                if (result.code == undefined) {                    
                    isLogin = false;
                    rcode = 2;
                    //alert("rcode is 2 :"+rcode);
                }
                if (result.code == 1) {                    
                    isLogin = true;
                    rcode = 1;
                    //alert("rcode is 1 :" + rcode);
                }
                if (result.code == 0) {
                    isLogin = false;
                    rcode = 0;
                    //alert("rcode is 0 :" + rcode);
                    document.location.href = "http://localhost:8870/docheck.aspx" ;
                    
                }
                if (result.code == -2) {                   
                    isLogin = false;
                    rcode = -2;
                    //alert("rcode is -2 :" + rcode);
                    document.location.href =  "http://localhost:8870/docheck.aspx";
                    
                }

                if (result.code == -1) {                    
                    isLogin = false;
                    rcode = -1;
                    //alert("rcode is -1 :" + rcode);
                    document.location.href = "http://localhost:8870/docheck.aspx";                   
                }


            });

            alert("return code :" + rcode);            
            //rcode = 1;

            return rcode;
        }


as the code ,when i alert the value of 'rcode', why the 'rcode' always returns 0 ?

but in the function , when i alert , it's value has changed . can't the global variable be changed in the ajax function ?
who can tell the truth ? thanks
Posted
Comments
ZurdoDev 4-Feb-15 22:33pm    
Yes, a global variable can be changed in there.

All you have to do is debug it and you'll see what is happening.
carono 4-Feb-15 22:47pm    
yes , i done what you said. but the value is always 0 . in the internal of the function , it is changed , but out of the function , it is 0 again.
ZurdoDev 5-Feb-15 7:32am    
Perhaps your page is reloading? That would reset it to 0.

the reason is when ajax post take time and before that you are returning from the CNY_CheckWeiDo method and having initial value as rcode
 
Share this answer
 
Comments
Santosh K. Tripathi 4-Feb-15 23:05pm    
Yes you right. yesterday i was facing same problem. After debugging i come to know this behavior of JS.
carono 4-Feb-15 23:10pm    
or add the success callback function such as :
$.ajax({
url:'url',
success:function(data){
rcode=1
}
});
carono 4-Feb-15 23:14pm    
okey . i will try it by using the $.ajax with the success callback.thanks you guys , S.K & DamithSL .
carono 4-Feb-15 23:07pm    
well, thanks damithsl. i think so . do you have some solutions about this issue? cancel the ajax ? use the Synchronous function instead of asynchronous?
replace alert("return code :" + rcode);

with
setTimeout (function (){ alert (rcode)}, 5000);


here 5000 milliseconds( 5 Seconds) is delay to run alert.

Actually here i am giving 5 seconds delay. So that alert execute after AJAX post can finish its work. Just decrease or increase time delay and see whats happens?
 
Share this answer
 
Comments
carono 5-Feb-15 1:47am    
thanks S.K. I have solved the issue. with the suggest by DamithSL . thanks for you again.
carono 5-Feb-15 2:03am    
yes , S.K , good idea! it is the problem i met . if i set the timer . it returns 0 first time , then it returns the right value at second time , so does the third time ,the fourth time and go on .
but also thank you for your mind.
Santosh K. Tripathi 5-Feb-15 3:17am    
welcome :)
thanks you all , it is a issue happened in ajax request.
in the internal of ajax , it requests with synchronous default . by your all 's help and mind , I set the ajax request with asynchronous by using
JavaScript
$.ajaxSetup({
    async : false
});


or such as :
JavaScript
$.ajax({ 
    type : "post", 
    url :'url', 
    data : {jsonparas}, 
    async : false, 
    success : function(data){ 
        //here you can change or assign the global variable
        
     } 
}); 


luckly , it worked .

thanks!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900