我想通过 AJAX 在我的 div
中加载一个 iFrame。我该怎么做?
<button id="iframeload">Load Iframe</button>
<div id="iframe_content"></div>
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$('.iframeload').click(function(event) {
event.preventDefault();
event.stopImmediatePropagation();
$.ajax({
type: "POST",
url : "https://pecom.ru/ru/calc/?iframe=Y",
success : function (data) {
// alert('ok');
$("#iframe_content").html(data);
}
});
});
</script>
这是 iFrame
<iframe id="pecom-kabinet-iframe" allowtransparency="true" frameborder="0" width="800" height="1800" scrolling="auto" style="border: 0;" src="https://pecom.ru/ru/calc/?iframe=Y">Not supported browser< / iframe >
和按钮 #iframeload
不起作用……
原文由 boniq 发布,翻译遵循 CC BY-SA 4.0 许可协议
第一个错误
您正在使用元素类,而不是代码中的 id:
第二个错误
看起来您希望 ajax 调用与您的页面所在的域不同。因此,浏览器会阻止它,因为出于安全原因,它通常允许同一来源的请求。
可能的解决方案
但是,如果你只想在按钮点击时显示 iframe 内容,则不需要 ajax。
只需使用
attr('scr',url)