确认 Ajax 返回的数据格式为 XML,并且包含根元素。
在 ASP.NET Core 5.0 中,可以通过以下方式设置返回的数据格式及内容:
(1)在 Controller 中返回一个 XML 结果:
[HttpPost]
public IActionResult AjaxTest()
{
XDocument xml = new XDocument(new XElement("root", new XElement("data", "Hello World!")));
return new XmlResult(xml);
}
(2)定义 XmlResult 类:
public class XmlResult : ContentResult
{
public XmlResult(XDocument xml)
{
Content = xml.ToString();
ContentType = "text/xml";
}
}