问题
Script
$.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.
This doesn't work.
while alert(xml) show xml File.
回答1:
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();
Additionally you could set dataType: 'xml'
but that's not necessary if your server is properly configured to send the correct content type.
Here's a live demo.
回答2:
Try forcing the dataType to xml: dataType: 'xml'
来源:https://stackoverflow.com/questions/5288173/get-xml-data-by-jquery-ajax