开发者

Adobe AIR and multiple XMLHttpRequests is being... weird

I'm making a small Adobe AIR app (my first) using HTML+Javascript. I need to run more than one asynchronous data request, but the second one didn't seem to be firing (note that the requests were not run concurrently originally). I tried stripping the program down to the bare minimum that exhibited problems, and at first only the first request fired, but then things got strange. Code and output follows:

<html> 
<head>
    <script type="text/javascript" src="AIRAliases.js"></script> 
    <script type="text/javascript">         
        function download(page) {
            var url = "http://en.wikipedia.org/w/api.php?action=parse&format=xml&page=" + page;
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", url,true);
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.开发者_运维知识库readyState == 4) {
                    air.trace("Done");
                }
            }
            xmlhttp.send(null);
        }

        function appLoad() { 
            download("Main Page");
            download("Main Page");
        }  
    </script> 
</head> 
<body onLoad="appLoad()"> 
</body> 
</html>

Expected output:

Done
Done

Actual output:

C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
Done
Done
Done
Done
Done
Done

Anyone seen anything like this before?


Simple answer, you shouldn't re-use xmlhttprequest objects (even if you don't realise you are because you're a complete noob at Javascript).

This line:

            xmlhttp = new XMLHttpRequest();

Should be:

            var xmlhttp = new XMLHttpRequest();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜