度日jquery.ajax XML数据
问题描述:
剧本
$.ajax({
type: "post",
url: "Default.aspx?cmd=Setting",
success: parseXml
});
function parseXml(xml)
{
alert(xml);//show Full XML File
//find every Tutorial and print the author
$(xml).find("Tutorial").each(function()
{
$("#a").append($(this).attr("author") + "<br />");
});
}
HTML
<div id="a"></div>
code
protected void Page_Load(object sender, EventArgs e)
{
if (Request["cmd"] == "Setting")
{
string k=@"<?xml version='1.0' encoding='utf-8' ?>
<RecentTutorials>
<Tutorial author='The Reddest'>
<Title>Silverlight and the Netflix API</Title>
<Categories>
<Category>Tutorials</Category>
<Category>Silverlight 2.0</Category>
<Category>Silverlight</Category>
<Category>C#</Category>
<Category>XAML</Category>
</Categories>
<Date>1/13/2009</Date>
</Tutorial>
</RecentTutorials>";
Response.Write(k );
Response.End();
}
}
我是一个初学者。
I am a beginner.
这是行不通的。
而警报(XML)显示XML文件。
while alert(xml) show xml File.
答
设置才能有jQuery的自动解析XML服务器上的适当的内容类型:
Set the proper content type on your server in order to have jQuery automatically parse the XML:
Response.ContentType = "text/xml";
Response.Write(k);
Response.End();
此外,你可以设置数据类型:XML
,但如果你的服务器配置正确发送正确的内容类型是没有必要
Additionally you could set dataType: 'xml'
but that's not necessary if your server is properly configured to send the correct content type.
下面是一个现场演示。