2012-11-05 40 views
0

我的兄弟写的代码之前的工作,但我想我改变了他的错误,也许你可以看到他为什么不会返回XML数据。XML HTTP只需要返回未定义

function CheckFromTo(From,To) 
{ 
    //alert(From + "," + To); 
    var xmlHttp = null; 
    var Url = "http://www.fpl.co.il/bo/info/CheckFromTo.aspx?FROM=" + From + "&TO=" + To + ""; 
xmlHttp = new XMLHttpRequest(); 
    xmlHttp.onreadystatechange = ProcessRequest; 
    xmlHttp.open("GET", Url, true); 
    xmlHttp.send(null); 
    return (ProcessRequest()); 


    function ProcessRequest() 
    { 
      if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
      { 
        var response = xmlHttp.responseText; 
        return response;  
      } 
    } 
} 
+1

你是怎么调用CheckFromTo()函数的?你结果怎么样? – FixMaker

+0

问你兄弟看看'return response;' – KooiInc

回答

1

看看你的XMLHttpRequest对象

xmlHttp.open("GET", Url, true); <-- the true Boolean 

在打开的方法真正的布尔意味着你使用的是异步调用,这意味着你不能返回一个值。欢迎来到异步编程。

为什么它返回undefined?

当统计数据不是200且readystate不是4时,ProcessRequest中会发生什么?没什么,它什么也没有返回,因此没有定义。

在使用异步调用时,您需要使用回调函数。这意味着将你的逻辑分解成多个步骤。