AJAX超出最大调用堆栈大小

时间:2015-05-08 05:06:31

标签: javascript ajax

我有一个读取xml的javascript函数。从该函数,它调用第二个函数来提示用户更新起始价格值。它第一次成功地执行此操作然后出现此错误。 2 未捕获RangeError:超出最大调用堆栈大小。 43 未捕获的InvalidStateError:无法在'XMLHttpRequest'上执行'send':对象的状态必须为OPENED。

我不确定这里发生了什么?这是一个递归问题吗?如果是这样,我该如何解决这个问题?

这是javascript:

var xmlhttp=false;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

function loadXMLDoc()
{
var table
var i;
xmlhttp.open("GET","auction.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

table=("<table border='1'><tr><th>Item Name</th><th>Category</th><th>Start   Price</th></tr>");
var x=xmlDoc.getElementsByTagName("Product");
for (i=0;i<x.length;i++)
{ 
table+=("<tr><td>");
table+=(x[i].getElementsByTagName("ItemName")[0].childNodes[0].nodeValue);
iname=(x[i].getElementsByTagName("ItemName")[0].childNodes[0].nodeValue);
table+=("</td><td>");
table+=(x[i].getElementsByTagName("Owner")[0].childNodes[0].nodeValue);
iowner=(x[i].getElementsByTagName("Owner")[0].childNodes[0].nodeValue);
//document.getElementById('test').innerHTML=iowner;
table+=("</td><td>");
table+=(x[i].getElementsByTagName("StartPrice")[0].childNodes[0].nodeValue);
table+=("</td><td>");
table+="<input type=\"submit\" onclick=\"itembid('"+ iname + "','"+ iowner +"')\" value=\"Bid\">";
table+=("</td></tr>");
}
table+=("</table>");
document.getElementById('listinglist').innerHTML=table;
}

function itembid(iname,iowner)
{
var newbid = prompt("Please enter your bidding price");
var itemname = iname;
var ownername = iowner;
//document.getElementById('test').innerHTML=ownername;
//document.getElementById('test').innerHTML="AA";
xmlhttp.open("GET", "readxml.php?newbid=" + encodeURIComponent(newbid) + "&itemname=" + encodeURIComponent(itemname) + "&ownername=" + encodeURIComponent(ownername) +"&date="+ Number(new Date), true);
xmlhttp.onreadystatechange = loadXMLDoc;
xmlhttp.send();
}

1 个答案:

答案 0 :(得分:1)

您需要有一个新的覆盖请求请求,请尝试

function getXmlHttp() {
    var xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

function loadXMLDoc() {
    if (this.readyState != 4 || this.status != 200) {
        return;
    }

    var table
    var i;
    var xmlhttp = getXmlHttp();
    xmlhttp.open("GET", "auction.xml", false);

    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState != 4 || xmlhttp.status != 200) {
            return;
        }

        var xmlDoc = xmlhttp.responseXML;

        table = ("<table border='1'><tr><th>Item Name</th><th>Category</th><th>Start   Price</th></tr>");
        var x = xmlDoc.getElementsByTagName("Product");
        for (i = 0; i < x.length; i++) {
            table += ("<tr><td>");
            table += (x[i].getElementsByTagName("ItemName")[0].childNodes[0].nodeValue);
            iname = (x[i].getElementsByTagName("ItemName")[0].childNodes[0].nodeValue);
            table += ("</td><td>");
            table += (x[i].getElementsByTagName("Owner")[0].childNodes[0].nodeValue);
            iowner = (x[i].getElementsByTagName("Owner")[0].childNodes[0].nodeValue);
            //document.getElementById('test').innerHTML=iowner;
            table += ("</td><td>");
            table += (x[i].getElementsByTagName("StartPrice")[0].childNodes[0].nodeValue);
            table += ("</td><td>");
            table += "<input type=\"submit\" onclick=\"itembid('" + iname + "','" + iowner + "')\" value=\"Bid\">";
            table += ("</td></tr>");
        }
        table += ("</table>");
        document.getElementById('listinglist').innerHTML = table;
    };

    xmlhttp.send();
}

function itembid(iname, iowner) {
    var newbid = prompt("Please enter your bidding price");
    var itemname = iname;
    var ownername = iowner;
    //document.getElementById('test').innerHTML=ownername;
    //document.getElementById('test').innerHTML="AA";

    var xmlhttp = getXmlHttp();
    xmlhttp.open("GET", "readxml.php?newbid=" + encodeURIComponent(newbid) + "&itemname=" + encodeURIComponent(itemname) + "&ownername=" + encodeURIComponent(ownername) + "&date=" + Number(new Date), true);
    xmlhttp.onreadystatechange = loadXMLDoc;
    xmlhttp.send();
}