【问题标题】:Append html content in a div from remote end point using Javascript使用 Javascript 从远程端点在 div 中附加 html 内容
【发布时间】:2019-05-04 03:40:27
【问题描述】:

我有一个外部端点

localhost:5000 加载带有图像的简单 html 页面。一个烧瓶服务器正在那里运行,它提供 html 内容。

我有另一个名为 index.html 的 html 文件。在 index.html, 中,我有一个名为 movie-data 的 div。我想发出一个 Ajax 请求 localhost:5000 并将 html 内容附加到 index.html 的 div movie-data 中。

index.html:-

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

function init() {

$.ajax({
    dataType: "html",
    url: "http://localhost:5000",
    success: function (data) {
        console.log(data);
        $("#movie-data").html($(data).append(data));
    }
});

init();

</script>

<div id="movie-data"></div>

显示错误,

Uncaught SyntaxError: Unexpected end of input
init();

【问题讨论】:

  • 注意:想想$(data).append(data) 做了什么......这没有任何意义。
  • 如果你使用.html,它将替换旧数据,如果你使用追加,它将追加,检查我的答案
  • 让我知道它是否有效

标签: javascript python jquery html ajax


【解决方案1】:

没有找到function的结束标签

修复:-

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

function init() {

  $.ajax({
    dataType: "html",
    url: "http://localhost:5000",
    success: function (data) {
        console.log(data);
        $("#movie-data").append(data);
    }
  });
}

init();

</script>

<div id="movie-data"></div>

【讨论】:

  • 它正在工作。如果我想在 iframe 中追加数据,您有什么建议吗?
  • 在 iframe 中设置一个 id 假设为 framediv 的 div,然后在其中附加数据为 framediv.append(data)
猜你喜欢
  • 2010-11-26
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
相关资源
最近更新 更多