我的表单提交被阻止,因为框架已被沙盒化且未设置“允许表单”权限

Mat*_*olo 7 javascript hubspot

我正在尝试为 Hubspot 构建自定义表单和提交帖子。

我有以下代码

HTML

<head>
<script src="prezzi-form-submit.js"></script>
</head>
<body>

  <form class='form-inline' id='my-custom-form'>
    <div class="form-group">
      <input type='email' class='form-control' placeholder='Your email address' required>
    </div>
    <button class="btn btn-primary" type='submit'>Sign up</button>
  </form>

  <!-- Actual form that gets submitted to HubSpot -->
  <div class="hidden" id='hubspot-form'>
    <script charset="utf-8" src="//js.hsforms.net/forms/current.js"></script>
    <script>
      hbspt.forms.create({
        portalId: 'my-portal-id',
        formId: '92b9b82a-0da2-4e23-8a30-04541c05ce6d',
        onFormReady: function($form) {
          $form.attr('target', 'hubspot-iframe');
        }
      });
    </script>

    <!-- iFrame that data will get submitted to. This hack stops the page redirect. -->
    <iframe name="hubspot-iframe" id="hubspot-iframe" sandbox="allow-forms"></iframe>
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)

JS (prezzi-form-submit.js)

// // Send form data to HubSpot from the client.
function submitToHubSpot(data) {
  var $form = $('#hubspot-form form'),
      k;

  // Loop through each value and find a matching input.
  // NOTE: Doesn't support checkbox/radio.
  for (k in data) {
    $form.find("input[name='" + k + "']").val(data[k]);
  }

  $("form input:submit").trigger("click");
}

// Here's how you'd use this.
$('#my-custom-form').on('submit', function() {
  var formData = {};
  $(this).serializeArray().forEach(function(data) {
    formData[data.name] = data.value;
  });

  submitToHubSpot(formData);

  // We sent the data. Now do whatever else you want.
  alert('Gee, thanks Jonathan! Now I can focus on onboarding my customers with Appcues!');
  window.location.href = 'http://appcues.com';
})
Run Code Online (Sandbox Code Playgroud)

当我按下提交按钮时,我在控制台中收到以下错误

已阻止向“ ”提交表单,因为表单的框架已被沙盒化并且未设置“允许表单”权限。

正如你所看到的,我有

沙箱=“允许形式”

设置在 I 框架中,但它不起作用。我该如何解决这个错误?

Ben*_*fez 24

有时,当您单击应用程序中的链接时,打开的选项卡将禁用 javascript/沙盒。

关闭选项卡并在新选项卡中重新打开相同的 URL,它可能会起作用。


小智 2

在 Hubspot 上使用 iFrame 表单时遇到了同样的问题,并得到了相同的 JS 错误。发现这与使用 HS 设计工具的实时预览有关。

在顶部的下拉菜单中,有“带显示选项的实时预览”,然后是“不带显示选项的预览”。正是“带显示选项的预览”选项使其成为“沙盒”,尝试不带该选项的选项。希望这对某人有帮助。