通过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") + "
"); }); }
HTML
码
protected void Page_Load(object sender, EventArgs e) { if (Request["cmd"] == "Setting") { string k=@" Silverlight and the Netflix API Tutorials Silverlight 2.0 Silverlight C# XAML 1/13/2009 "; Response.Write(k ); Response.End(); } }
我是初学者。
这不起作用。
而alert(xml)显示xml文件。
在服务器上设置正确的内容类型,以便让jQuery自动解析XML:
Response.ContentType = "text/xml"; Response.Write(k); Response.End();
此外,您可以设置dataType: 'xml'
但如果您的服务器已正确配置为发送正确的内容类型,则不需要。
这是一个现场演示 。
尝试将dataType强制为xml: dataType: 'xml'