jQuery 版本:1.4.4
IE 版本: 8.0
错误信息: parsererror, TypeError: 缺少对象
test.xml
<?xml version="1.0" encoding="utf-8"?>
<rows>
<row>
<id>1</id>
<name>a.wav</name>
</row>
<row>
<id>2</id>
<name>b.wav</name>
</row>
<row>
<id>3</id>
<name>c.wav</name>
</row>
</rows>
test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: "test.xml",
dataType: "xml",
sucess: function(xml){
$(xml).find("row").each(function() {
alert($(this).find("name").text());
});
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
var errMsg = "通过 jquery 访问 XML 数据时发生错误!";
errMsg += "\nxmlHttpRequest.status = " + xmlHttpRequest.status;
errMsg += "\nxmlHttpRequest.readyState = " + xmlHttpRequest.readyState;
errMsg += "\ntextStatus = " + textStatus;
errMsg += "\nerrorThrown = " + errorThrown;
alert(errMsg);
}
});
});
</script>
</head>
<body>
jquery 访问 XML 的测试页面。
</body>
</html>
运行结果
解决方案
将 javascript 代码改为:
$.get("test.xml", function(data) {
var xml = null;
if ($.browser.msie) {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
}
$(xml).find("row").each(function() {
alert($(this).find("name").text());
});
});
相关阅读:XMLHttpRequest 对象(AJAX)的状态码(readyState)及HTTP 状态代码(status)的对照表