多个XMLHttpRequests不起作用

时间:2014-10-09 06:47:14

标签: javascript html ajax xmlhttprequest

我对此感到困惑。我有两个XMLHttpRequests操作我的HTML文件的Select元素(每个加载HTML文件时,每个元素都在不同的Select元素上运行)。我正在使用W3CSchools推荐的回调函数。如果我的变量xmlHttp是在我的回调函数之外定义的,那么只有第二个请求有效,并且第一个请求在有机会完成之前被删除。如果我把'var'放在它前面,同样的事情发生了。 然而,如果我的变量在函数内部,前面有'var',那么绝对没有任何反应。我把它缩小到了“HERE !!!!!”这一行的哪个地方程序似乎挂起的地方。我知道loadXMLDoc函数实际上并没有完成,因为当我在它之外发出警报时,没有任何反应。我认为它与'if'部分和程序无法识别xmlHTTP有关,即使它是本地定义的。我仍然是JavaScript的新手,只是希望能够一次运行多个XMLHttpRequest对象,而不会让对方进入,但也不会挂起页面。任何想法为什么这不起作用?

HTML:

<form>

    <select id="stateSelectCities">
        <!-- Will be populated with MySQL -->
    </select>

    <select id="citySelect">
        <option>Select a State</option>
    </select>

    <br />
    <br />

    <select id="stateSelectCounties">
        <!-- Will be populated with MySQL -->
    </select>

    <select id="countySelect">
        <option>Select a State</option>
    </select>

    <p id="xmltest"></p>
    <p id="currentState"></p>
    <p id="sc"></p>
    <p id="rs"></p>
    <p id="st"></p>

</form>

JavaScript的:

<script type="text/javascript">
function loadXMLDoc(method, data, url, cfunc) {
            var xmlHTTP = new XMLHttpRequest();
            xmlHTTP.onreadystatechange = cfunc;
            xmlHTTP.open(method, url, true);
            if (data) {
                xmlHTTP.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlHTTP.send(data);
            } else {
                xmlHTTP.send();
            }
        }

function returnStateListForCounties() {
            loadXMLDoc('GET', null, "stateslist.xml", function() {
                document.getElementById('countySelect').disabled = true;
                if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {

                    // Read the XML Data and Populate Counties States Menu
                    var response = xmlHTTP.responseXML;
                    var states = response.getElementsByTagName('state');
                    for (i = 0; i < states.length; i++) {
                        var option = document.createElement('option');
                        option.innerHTML = states[i].childNodes[0].nodeValue;
                        option.setAttribute('onmouseup', 'returnCounties(this.innerHTML)');
                        document.getElementById("stateSelectCounties").add(option);
                    }
                }
                //document.getElementById("sc").innerHTML = 'statusCode: ' + xmlHTTP.status;
                //document.getElementById("rs").innerHTML = 'readyState: ' + xmlHTTP.readyState;
                //document.getElementById("st").innerHTML = 'statusText: ' + xmlHTTP.statusText;

            })
        }

function returnStateListForCities() {
            loadXMLDoc('GET', null, 'stateslist.xml', function() {
                document.getElementById('citySelect').disabled = true;
                // HERE!!!!!
                if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {

                    // Read the XML Data and Populate Cities States Menu
                    var response = xmlHTTP.responseXML;
                    var states = response.getElementsByTagName('state');
                    for (i = 0; i < states.length; i++) {
                        var option = document.createElement('option');
                        option.innerHTML = states[i].childNodes[0].nodeValue;
                        option.setAttribute('onmouseup', 'returnCities(this.innerHTML)');
                        document.getElementById("stateSelectCities").add(option);
                    }
                }
                document.getElementById("sc").innerHTML = 'statusCode: ' + xmlHTTP.status;
                document.getElementById("rs").innerHTML = 'readyState: ' + xmlHTTP.readyState;
                document.getElementById("st").innerHTML = 'statusText: ' + xmlHTTP.statusText;

            })
        }

//returnStateListForCounties();
returnStateListForCities();

</script>

1 个答案:

答案 0 :(得分:1)

这里的问题是xmlHTTP变量,它在loadXMLDoc函数中定义并尝试在returnStateListForCounties函数内再次使用,我会这样做:

       function loadXMLDoc(method, data, url, cfunc) {
            var xmlHTTP = new XMLHttpRequest();
            xmlHTTP.onreadystatechange = function() {
                if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200)
                {                         
                    cfunc(xmlHTTP.responseXML); //Call passed func with the resulting XML
                }
            };

            xmlHTTP.open(method, url, true);
            if (data) {
                xmlHTTP.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlHTTP.send(data);
            } else {
                xmlHTTP.send();
            }
        }

这样就封装了数据恢复。