2017-03-24 25 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'引用出它的范围。 –

相关问题