我正在 IBM worklight 中开发一个应用程序。通过 Ajax,我必须连接到网站并从中检索数据,但它给出了 thisd 错误:
未捕获的类型错误:无法在 file:///data/data/com.Test/files/www/default/Test.html:80 处调用 null 的方法“getElementsByTagName”
我该如何解决?我的代码是这个
<!DOCTYPE HTML>
<html>
<head>
<script>window.$ = window.jQuery = WLJQ;</script>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel="stylesheet" href="css/Test.css">
<script>
function loadXMLDoc()
{
var xmlhttp;
var txt,x,i;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
txt="";
// x=xmlDoc.getElementsByTagName("Id");
var table = xmlDoc.getElementsByTagName("table");
var tds = table.getElementsByTagName("td");
alert(tds);
for (var i = 0; i < tds.length; i++) {
alert(tds[i].innerHTML);
}
/* for (i=0;i<x.length;i++)
{
txt=txt + x[i].childNodes[0].nodeValue + "<br>";
} */
document.getElementById("myDiv").innerHTML=txt;
}
}
xmlhttp.open("GET","http://www.marketing.com/msb_en.html",true);
xmlhttp.send();
}
</script>
</head>
<body id="content" style="display: none;">
<h2>MCollection:</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">collection</button>
<!--<input type="button" value="button name" onclick="window.open('https://www.google.com.pk')" /> -->
<!--application UI goes here-->
<script src="js/initOptions.js"></script>
<script src="js/Test.js"></script>
<script src="js/messages.js"></script>
</body>
</html>
我的第 80 行是
var table = xmlDoc.getElementsByTagName("table");