【发布时间】:2019-04-05 15:56:03
【问题描述】:
更新
问题不在于 iframe,问题在于发布 json 的 onsubmit 函数未提交表单。目标是动态创建一个 iframe,该 iframe 使用表单发布重定向到具有上述脚本标记的 json 内容的另一个 URL。
原创
我在示例网站上有以下内容:
<script data-dashboard="true" type="application/json">
{
"title":"Serverless Identity"
}
</script>
<script type="text/javascript">
let dashboardConfiguration = document.querySelector("script[data-dashboard=\"true\"]");
if (dashboardConfiguration) {
let iframe = document.createElement("iframe");
let model = JSON.stringify(JSON.parse(dashboardConfiguration.innerHTML.trim()));
document.body.appendChild(iframe);
var doc = iframe.contentWindow.document;
doc.open()
doc.writeln(`<form id="form" action="https://localhost:44338/dashboard/" method="POST" target="_self"></form>`)
doc.close();
iframe.onload = () => {
let form = doc.getElementById("form");
form.addEventListener("submit", (e) => {
console.log(model);
e.preventDefault();
// construct an HTTP request
var xhr = new XMLHttpRequest();
xhr.open(form.method, form.action, true);
xhr.setRequestHeader('content-type', 'application/json; charset=UTF-8');
// send the collected data as JSON
xhr.send(model);
xhr.onloadend = function () {
// done
};
});
form.submit();
}
};
</script>
【问题讨论】:
-
IFRAME 的
onload事件不可靠。此外,您不应将箭头函数用于事件侦听器 - 尝试将onsubmit与普通函数一起使用(类似于<form onsubmit="return ajaxSubmit(event)">,然后在此函数中返回 false) -
您的建议没有任何改变。
-
所以您正在创建一个表单,向该表单添加一个提交处理程序,从脚本提交该表单,然后您在提交处理程序中发出一个 AJAX 请求。为什么?为什么不直接发出 AJAX 请求并完成它,这个表单的目的是什么……?
-
没有表单就不能重定向页面吗?
标签: javascript