在异步xhttp数据库访问之后,Javascript函数调用不会返回

时间:2017-05-25 07:36:29

标签: javascript asynchronous

该功能根本没有返回,因此跟随它的代码不会运行。就异步xmlhttp请求而言,我缺少什么? 功能如下。控制台将显示"完成1"和"完成2"但不是"完成3"



function _getInventory() {
    //This function
    console.log("The function is running");
    var xmlhttpr = new XMLHttpRequest();
    xmlhttpr.responseType = "document";
    xmlhttpr.onreadystatechange = function() {
        if(this.readyState==4 && this.status==200) {//The request was fulfilled
            var xmlDoc = this.responseXML;
            console.log(xmlDoc);
            var returnStatus = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
            if(returnStatus==0) {//Results were found
                //get an itemNodeList object
                itemNodeListr = xmlDoc.getElementsByTagName("Items")[0].getElementsByTagName("Item");
                //Purge the 'html' variable of previous search data
                var invDisplay = document.getElementById("inventory-display");
                var html = "<div class='item-slide' onclick='javascript:add_to_inventory()' id='edit-inv-data'>";
                html+="<img src='../icons/add.png'>"
                html+="</div>"; //APPROPRIATE ID
                invDisplay.innerHTML=html;

                if(itemNodeListr.length>0) {
                    var i = 0;
                    var html="";
                    for(i=0;i<itemNodeListr.length; i++) {
                        html="<div class='item-slide'>";
                        html+="<div class='item-slide-image'>";
                        html+="<img src='"+getValue(itemNodeListr, i, 'ImageURI')+"'>";
                        html+="</div><!--item-slide-header-->"
                        html+="<div class='item-slide-content' id='itemid"+i+"'>"
                        html+="<table>";
                        html+="<tr>";
                        html+="<th>Name</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'ItemName')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>Other Names</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'Aliases')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>Description</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'Description')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>Quantity</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'Quantity')+" "+getValue(itemNodeListr, i, 'Units')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>Unit Price</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'UnitPrice')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>State</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'State')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>Description</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'Description')+"</td>";
                        html+="</tr>";
                        html+="<tr>";
                        html+="<th>Added On</th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'DateAdded')+"</td>";
                        html+="</tr>";
                        html+="<th>Can Deliver? (Y/N) </th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'Deliverable')+"</td>";
                        html+="</tr>";
                        html+="<th>Can Deliver To: </th>";
                        html+="<td>"+getValue(itemNodeListr, i, 'DeliverableAreas')+"</td>";
                        html+="</tr>";
                        html+="</table>";
                        html+="</div><!--item-slide-header-->"
                        html+="<div id='addToRep'>";//ID means 'Add to repository'
                        html+="<button onclick='void(0)'><i class='fa fa-edit'></i> Edit</button>";
                        html+="</div>";
                        html+="<span onclick='rem_rep_item("+i+")' id='rem-rep-item"+i+"' class='close' title='Delete Item'>×</span>";
                        html+="</div>";
                        document.getElementById("inventory-display").innerHTML+=html;
                    }
                    console.log("Done 1");
                }

            } else if(returnStatus==1) {//No Results found
                console.log("No results were found");
            } else if(returnStatus==3) {//Problem connecting to the database
                console.log("There was a problem connecting to the database");
            } else if(returnStatus==11) {//User is not logged in. Not even sure how that's possible
                console.log("WTF? Is that even possible");
            }
        } else {//The request wasn't fulfilled for some reason
            console.log("ReadyState = "+this.readyState);
            console.log("Status = "+this.status)
        }
        console.log("Done 2");
    }
    xmlhttpr.open("GET", "xhttp.php?table=Repository", true);
    xmlhttpr.send();
    console.log("Done 3");
}
&#13;
&#13;
&#13;

0 个答案:

没有答案