2017-03-24 27 views
-1

不知何故,這段代碼只是空白,並沒有真正做任何事情。 甚至不顯示任何錯誤。 那麼有人可以幫忙嗎?不知何故,這不會告訴我一個提醒或任何錯誤

function connectStart(mycon){ //connection start for contacting 
    return function(){ 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
     var jsonReturn = JSON.parse(this.responseText); 

     mycon; //calls the makeAnn() Function 

     } 
    }; 
// mypref() stands for the preference code location for the myphp.php 
xmlhttp.open("GET", mypref()+"myphp.php?q="+myvar,true); 
xmlhttp.send(); 
     } 
} 

function makeAnn(){ 
    return function(){ 
    console.log(jsonReturn); 
    if (jsonReturn !== "NO"){ 
      alert("Announcement Was Posted"); 
    } else { 
     alert("Error Encoding Data"); 
    } 
     } //end of return function() 
} 

function mainFunction(){ //is called by an onclick event 
var myvar = "I Shall Return!"; 
connectStart(makeAnn()); // i used invocation to combine them 
} 

不知何故,它從不顯示任何實際的投訴或控制檯日誌上的任何內容。 沒有警報或任何。 不寫數據發送到php或數據庫。 沒有什麼真的。 我試過了PHP和HTML,他們都很好。 這只是我的代碼的這一部分,將無法正常工作。

+0

你什麼時候調用'mainFunction()'..?如果這是你的所有代碼,那麼你並沒有調用任何代碼。請格式正確,非常難以閱讀。理想情況下,請創建一個小提琴或類似的,所以我們可以看到這種情況發生。 http://stackoverflow.com/help/how-to-ask –

+0

這裏沒有「php」這裏 –

+0

剛完成編輯 – Arkonsol

回答

0

你沒有調用你的onreadystatechange處理程序中的函數。見下文。

function connectStart(mycon){ //connection start for contacting 
    return function(){ 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
     var jsonReturn = JSON.parse(this.responseText); 

     // mycon; //wouldn't do anything 
      mycon(); // THIS should work better... 
     } 
    }; 
    // mypref() stands for the preference code location for the myphp.php 
    xmlhttp.open("GET", mypref()+"myphp.php?q="+myvar,true); 
    xmlhttp.send(); 
    } 
} 

編輯: 我現在注意到的另一個問題是,你引用jsonReturn辦法,出路其範圍。

function connectStart(mycon){ //connection start for contacting 
    return function(){ 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
     var jsonReturn = JSON.parse(this.responseText); 

     mycon(jsonReturn); // pass jsonReturn as a parameter 

     } 
    }; 
// mypref() stands for the preference code location for the myphp.php 
xmlhttp.open("GET", mypref()+"myphp.php?q="+myvar,true); 
xmlhttp.send(); 
     } 
} 

function makeAnn(){ 
    return function(jsonReturn){ // pass in as a parameter 

    console.log(jsonReturn); 
    if (jsonReturn !== "NO"){ 
      alert("Announcement Was Posted"); 
    } else { 
     alert("Error Encoding Data"); 
    } 
     } //end of return function() 
} 

function mainFunction(){ //is called by an onclick event 
var myvar = "I Shall Return!"; 
connectStart(makeAnn()); // i used invocation to combine them 
} 
+0

nope仍然沒有:/它是如此奇怪.... – Arkonsol

+0

你肯定(從瀏覽器的devtools看)HTTP請求返回?對不起,我現在也看到另一個問題。 –

+0

@Arkonsol請參閱最新的答案。你也試圖將'jsonReturn'引用出它的範圍。 –

相關問題