【问题标题】:get xml data by jquery.ajax通过 jquery.ajax 获取 xml 数据
【发布时间】:2011-07-14 09:01:30
【问题描述】:

脚本

  $.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>

代码

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();
    }
}

我是初学者。

这不起作用。

while alert(xml) 显示 xml 文件。

【问题讨论】:

  • 请定义“不起作用”。
  • 你试过alert(xml);里面的parseXml
  • 显示 xml 但不附加到 div

标签: asp.net xml jquery xmlhttprequest


【解决方案1】:

在您的服务器上设置正确的内容类型,以便让 jQuery 自动解析 XML:

Response.ContentType = "text/xml";
Response.Write(k);
Response.End();

此外,您还可以设置 dataType: 'xml',但如果您的服务器配置正确以发送正确的内容类型,则没有必要这样做。

这是live demo

【讨论】:

    【解决方案2】:

    尝试将 dataType 强制为 xml:dataType: 'xml'

    【讨论】:

    • 因此应在服务器代码中额外使用Response.ContentType = "text/xml; charset=utf-8";
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多