jQuery ajax xml response剥离了xml标签



服务器返回以下 XML:

<Person>
  <FirstName>John</FirstName>
  <LastName>Buttler</LastName>
  <Age>49</Age>
</Person>

在这个 ajax 方法中

function SetContent(selectedVal) {
    $.ajax({
        type: "GET",
        url: "/Home/GetTestRecordContent",
        data: { testRecordId: selectedVal },
        dataType: 'xml',
    }).done(function (result) {
        if (result) {
            $(result).each(function () {
                $("#TestRecordContent").text($(this).text());
            });
        };
    })

我只收到没有 XML 标记的文本:

John
Buttler
49

如何获取包含所有 XML 标记的完整 XML 文档?

正如BDeliers所说,将类型设置为文本(或者只是删除数据类型选项,并将其保留为默认值),它应该可以根据需要工作。

还要确保你不使用 $(this).text(),而只是在你的 jquery 调用中使用 'result':

$.ajax({
    type: "GET",
    url: "/Home/GetTestRecordContent",
    data: { testRecordId: selectedVal }
}).done(function (result) {
  $("#TestRecordContent").text(result);
});

如果您只是在请求中将数据类型设置为文本,它将返回包含所有标记的 XML

最新更新