【问题标题】:Want to display XML received from post request as HTML想要将从 post 请求收到的 XML 显示为 HTML
【发布时间】:2020-09-02 18:16:32
【问题描述】:

我目前正在运行一个从另一个网站检索 XML 文档的函数。我能够在包含在#document 中的console.log 中显示返回的XML。

当我尝试解析 XML 并将其添加到 dom 时,我收到错误“无法读取属性 'ownerDocument' of null”。

我的目标是将 xml 代码打印到 dom 上,以便显示 HTML 中带有“结果”标签的所有内容并设置样式,但我在 XML 解析方面做错了。

这是目前为止的代码:

getData();

function getData() {
$.post(url)
    .then(function (response) {
      console.log(response);
      var xmlDoc = $.parseXML( response ),
      $xml = $( xmlDoc ),
      $title = $xml.find( "results" );
      $( "p.test" ).append( response );
   })   
    .fail(function(xhr, textStatus, error) {
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error.responseJSON)
   });
}

【问题讨论】:

  • 如果 console.log 将其显示为文档,则不要调用 $.parseXML,因为它已经是 xml 文档
  • 谢谢!删除 parseXML 有效!

标签: html jquery ajax xml post


【解决方案1】:

正如 Musa 在我的问题的 cmets 中指出的那样,由于我已经收到了 XML 文档,因此没有理由解析 XML。对代码的以下更改完美运行:

dataToHubspot();

function dataToHubspot() {
$.post(url)
    .then(function (response) {
      console.log(response);
      var xml = $( response ),
      $title = xml.find( "results" );
      $( "p.test" ).append( $title );
   })   
    .fail(function(xhr, textStatus, error) {
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error.responseJSON)
   });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多