Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法读取属性';getElementsByTagName';在XMLHttpRequest.getData处为null_Javascript_Jquery - Fatal编程技术网

Javascript 未捕获类型错误:无法读取属性';getElementsByTagName';在XMLHttpRequest.getData处为null

Javascript 未捕获类型错误:无法读取属性';getElementsByTagName';在XMLHttpRequest.getData处为null,javascript,jquery,Javascript,Jquery,这是我正在尝试运行的代码/函数: function loadXMLDoc(cart) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",cart,false); xhttp

这是我正在尝试运行的代码/函数:

function loadXMLDoc(cart)
{
    if (window.XMLHttpRequest)
      {
        xhttp=new XMLHttpRequest();
      }
    else
      {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",cart,false);
    xhttp.send();
    return xhttp.responseXML;
}

function getData()
{   
    if ((xhr.readyState == 4) &&(xhr.status == 200))
        {   
            var serverResponse = xhr.responseXML;
            var header = serverResponse.getElementsByTagName("book");
            var spantag = document.getElementById("cart");
            var x;
            spantag.innerHTML = "";
            x = "<table cellpadding='1' cellspacing='6' border='0'>";
            x += "<tr><td>Title</td><td>ISBN</td><td>Qty</td><td>Total</td><td>Remove</td></tr>";
            for (i=0; i<header.length; i++)
            {  
                var id =  header[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
                var total =  header[i].getElementsByTagName("total")[0].childNodes[0].nodeValue;
                var title =  header[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
                var isbn =  header[i].getElementsByTagName("isbn")[0].childNodes[0].nodeValue;
                var qty =  header[i].getElementsByTagName("quantity")[0].childNodes[0].nodeValue;
                if(qty=="0")
                {
                    continue;
                }
                x += "<tr>"
                + "<td>" + title + "</td>"
                + "<td>" + isbn + "</td>"
                + "<td>" + qty + "</td>"
                + "<td>" + total + "</td>"
                + "<td>" + "<a href='#' onclick='AddRemoveItem(\"Remove\","+id+");'>Remove Item</a>" + "</td>"
                + "</tr>";

            }
            x += "</table>";
            if (header.length != 0)
                spantag.innerHTML = x;
        }
}
函数加载xmldoc(购物车)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、cart、false);
xhttp.send();
返回xhttp.responseXML;
}
函数getData()
{   
如果((xhr.readyState==4)和&(xhr.status==200))
{   
var serverResponse=xhr.responseXML;
var header=serverResponse.getElementsByTagName(“book”);
var spantag=document.getElementById(“购物车”);
var x;
spantag.innerHTML=“”;
x=“”;
x+=“TitleISBNQtyTotalRemove”;

对于(i=0;i是否在同一服务器/域上调用脚本?避免跨域请求!另一件事是
responseXML
需要完全有效的XML,并且可能需要从服务器发送的响应头中正确的
内容类型。