Asp.net 通过jquery.ajax获取xml数据
剧本Asp.net 通过jquery.ajax获取xml数据,asp.net,xml,jquery,xmlhttprequest,Asp.net,Xml,Jquery,Xmlhttprequest,剧本 $.ajax({ 类型:“post”, url:“Default.aspx?cmd=Setting”, 成功:parseXml }); 函数解析xml(xml) { 警报(xml);//显示完整的xml文件 //查找每个教程并打印作者 $(xml).find(“教程”).each(函数() { $(“#a”).append($(this.attr(“作者”)+”); }); } HTML 代码 受保护的无效页面加载(对象发送方,事件参数e) { 如果(请求[“cmd”]=“设置”)
$.ajax({
类型:“post”,
url:“Default.aspx?cmd=Setting”,
成功:parseXml
});
函数解析xml(xml)
{
警报(xml);//显示完整的xml文件
//查找每个教程并打印作者
$(xml).find(“教程”).each(函数()
{
$(“#a”).append($(this.attr(“作者”)+”
);
});
}
HTML
代码
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(请求[“cmd”]=“设置”)
{
字符串k=@“
Silverlight和Netflix API
教程
Silverlight 2.0
银光
C#
XAML
1/13/2009
";
回答:写(k);
Response.End();
}
}
我是初学者
这不管用
当警报(xml)显示xml文件时。尝试将数据类型强制为xml:
数据类型:“xml”
尝试将数据类型强制为xml:数据类型:“xml”
在服务器上设置适当的内容类型,以便jQuery自动解析xml:
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();
}
}
此外,您可以设置数据类型:“xml”
,但如果您的服务器正确配置为发送正确的内容类型,则不需要设置
下面是一个。在服务器上设置适当的内容类型,以便jQuery自动解析XML:
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();
}
}
此外,您可以设置数据类型:“xml”
,但如果您的服务器正确配置为发送正确的内容类型,则不需要设置
这里有一个。请定义“不起作用”。您是否尝试过
警报(xml)代码>内部parseXml
显示xml但不附加到div请定义“不工作”。您是否尝试了警报(xml)代码>内部parseXml
显示xml,但不附加到div,因此应另外使用Response.ContentType=“text/xml;charset=utf-8”
在服务器代码中,作为后续代码,还应使用Response.ContentType=“text/xml;charset=utf-8”服务器代码中的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();
}
}
Response.ContentType = "text/xml";
Response.Write(k);
Response.End();