动态iframe中的表单onsubmit不会被触发

时间:2019-04-05 06:41:19

标签: javascript

更新

问题不是iframe,而是问题是发布json的onsubmit函数未提交表单。目标是动态创建一个iframe,该iframe可以使用表单发布重定向到具有上述script标签的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>

我还尝试了onsubmit,其结果与普通提交相同。 enter image description here

3 个答案:

答案 0 :(得分:1)

不可能

根据规范,使用form.submit()时,任何onsubmit处理程序都不会触发。

我使用了另一种方法,即使用表单编码的值发送常规表单提交,其中空有效载荷位于一个隐藏字段中,然后在服务器端反序列化。

答案 1 :(得分:0)

创建iframe后,将其加载,然后绑定onload,而不是像

那样将iframe附加到主体,则应在主体上使用DOMNodeInserted事件。
let iframe = document.createElement("iframe");
        let model = JSON.stringify(JSON.parse(dashboardConfiguration.innerHTML.trim()));
document.body.addEventListener("DOMNodeInserted", function (ev) {
//write your logic here
});
        document.body.appendChild(iframe);

        var doc = iframe.contentWindow.document;

答案 2 :(得分:0)

删除iframe.onload = () => { }。检查镶边network标签。表单网址https://localhost:44338/dashboard/被触发。

<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();

        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();
    }