2014-11-25 84 views
0

我是網絡開發新手。創建一個註冊頁面,對php進行一些異步調用。 Ran調試發現控件完全跳過onreadystatechange函數。請幫助...控件跳過ajax對象的onreadystatechange函數。爲什麼?

var ajax = ajaxObj("POST", "signup.php");  //defines the ajax object, definition is below 
    ajax.onreadystatechange = function() {  //doesn't run after this line 
     if(ajaxReturn(ajax) == true) { 
      if(ajax.responseText != "signup_success"){ 
       status.innerHTML = ajax.responseText; 
       _("signupbtn").style.display = "block"; 
      } else { 
       window.scrollTo(0,0); 
       _("signupform").innerHTML = "OK "+u+", check your email inbox and junk mail box 
        at <u>"+e+"</u> in a moment to complete the sign up process."; 
      } 
     } 
    } 
    ajax.send("u="+u+"&e="+e+"&p="+p1+"&c="+c+"&g="+g);  //control reaches here directly 
} 
}// control exits here 

創建外部這裏的Ajax對象..

function ajaxObj(meth, url) { 
var x = new XMLHttpRequest(); 
x.open(meth, url, true); 
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
return x; 
} 

function ajaxReturn(x){ 
    if(x.readyState == 4 && x.status == 200){ 
    return true;  
    } 
} 

回答

0

這是因爲它是一個事件的回調函數,當服務器響應您的Ajax請求會被調用。如果您使用的是Firefox,請按F12,切換到網絡選項卡並檢查html和xhr以查看其狀態。

0

因爲它是異步的,所以當您以線性方式遍歷代碼時,函數將不會被調用。

當就緒狀態改變時,它被本地代碼調用。

如果要調試它,請在函數內部粘貼一個斷點。

相關問題