【发布时间】:2009-06-09 09:11:07
【问题描述】:
我正在尝试学习 JQuery - 我对 ajax 有一个小问题。我正在尝试使用从页面的 XML 响应返回的值填充 javascript 数组。
这是我的主页(ajax.html):
<html>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="jquery/fiber.js"></script>
</head>
<body>
<p>Ajax</p>
<script>
var ringType = new Array();
</script>
</body>
</html>
fiber.js 是这样的:
//process things for fiber map
jQuery(document).ready(function() {
// do stuff when DOM is ready
//populate and display ringType
$.ajax({
type: "GET",
url: "ajaxHelper.pl",
data: {
getRingTypes: "1",
},
dataType: "xml",
success: function(xml) {
//if the query was successfull,
alert("Got an xml object:"+$(xml));
$(xml).find("ringType").each( function(){
alert("Received reply "+$(this).text());
var type = $(this).html(); //save the value
//append to ringType array
ringType.push(type);
});
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
for(var i=0; i<ringType.length; i++){
document.write("<br>"+ringType[i]);
}
});
ajaxHelper.pl 生成这个 XML(在 \? 中没有反斜杠)(作为内容类型文本/xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<\?xml version="1.0" encoding="ISO-8859-1"\?>
<ringType>IA</ringType>
<ringType>IL</ringType>
<ringType>IN</ringType>
<ringType>IR</ringType>
<ringType>RT</ringType>
问题是,每次加载ajax.html,ajax查询都是成功的,但是却执行了error函数! xhr.status = 200(表示查询正常)并且 throwedException 未定义。
【问题讨论】:
-
抱歉,xml 没有正确粘贴。又来了(有一些空格):
IA IL IN IR RT -
尝试将 放在输出 XML 文档的顶部(如果还没有的话)
-
如果您在浏览器 (mywebapp/ajaxHelper.pl) 中打开 ajaxHelper.pl 文件,浏览器是否会将来自该文件的响应识别为 XML?