get xml data by jquery.ajax

允我心安 提交于 2020-01-16 00:47:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!