0

the code my brother wrote worked before but i think i changed him by mistake maybe you can see why he wont return the XML data.

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;    
            }
    }
}
4

1 回答 1

1

查看您的 XMLHttprequest 对象

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

open 方法中的真正布尔值意味着您正在使用异步调用,这意味着您不能返回值。欢迎来到异步编程。

为什么它返回未定义?

当 stats 不是 200 且 readystate 不是 4 时,ProcessRequest 会发生什么?什么都没有,它什么都不返回,因此未定义。

处理异步调用时需要使用回调函数。这意味着将您的逻辑分解为多个步骤。

于 2012-11-05T14:44:39.253 回答