我需要从服务器获取一个xml字符串,格式为:
<xml><material>
<record>
<id>..</id>
<name>..</name>
<quantity>..</quantity>
<plink>..</plink>
</record>
<record>
..
</record>
</xml></material>
客户端获取此xml并处理它:
$.ajax({
url:'getrecords.php',
cache:false,
type:"GET",
dataType:'html',
success:function (html){
var records=$(html).find("record");
alert(records.length);
}
我的问题是,这个代码在Chrome和Firefox中运行良好但在IE 8.0中没有运行(我使用的是8.0)它在IE中警告0,同时它在chrome和firefox中提示正确的长度。
我试试这个,但它也显示IE中的记录长度为0但Chrome中的记录很短
var records=$("<xml><root><item></item></root></xml>").find("item");
答案 0 :(得分:1)
如果返回的是xml,为什么要将dataType设置为html?尝试使用dataType'xml'
答案 1 :(得分:1)
<!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>
<title></title>
<script src="../../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var xml = '<?xml version="1.0" encoding="utf-8" ?><material><record><id>1</id><name>A</name><quantity>10</quantity>'
+ '<plink>test</plink></record><record><id>2</id><name>B</name><quantity>20</quantity><plink>test1</plink>'
+ '</record><record><id>3</id><name>C</name><quantity>34</quantity><plink>test2</plink></record><record>'
+ '<id>4</id><name>B</name><quantity>45</quantity><plink>test6</plink></record></material>';
// $.ajax({
// url: 'getrecords.php', cache: false, type: "GET", dataType: 'html',
// success: function (xml) {
var data = $($.parseXML(xml));
var records = $(data).find('record');
alert(records.length);
// }
// });
});
</script>
</head>
<body>
<?xml version="1.0" encoding="utf-8" ?>
<material>
<record>
<id>1</id>
<name>A</name>
<quantity>10</quantity>
<plink>test</plink>
</record>
<record>
<id>2</id>
<name>B</name>
<quantity>20</quantity>
<plink>test1</plink>
</record>
<record>
<id>3</id>
<name>C</name>
<quantity>34</quantity>
<plink>test2</plink>
</record>
<record>
<id>4</id>
<name>B</name>
<quantity>45</quantity>
<plink>test6</plink>
</record>
</material>
</body>
</html>