【问题标题】:Form submit to new page with javascript json array data表单提交到带有javascript json数组数据的新页面
【发布时间】:2022-01-25 15:20:36
【问题描述】:

fetch 的使用非常普遍,但它不会加载新页面的数据,它只是发送数据和接收数据。

如何像普通的 HTML 表单提交一样使用它,但我可以像 fetch 一样发送数据,但可以像表单操作一样打开新链接,而不是使用名称。打开新页面。

如果我使用表单操作,我必须使用 name 属性,但是有一些数据,所以 name 可能太多了,它也可以是数组,这对于使用 name 来说是困难的。

是否可以发送像 html 这样的数据,但没有名称,打开一个新页面,所有数据都是通过发送的。

只有我想发送一个数组作为 json 数据发布到新页面。

<!-- Here is example of using alpine and using fetch to send data it return whole page but i can't do anything with it -->
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
<div x-data="{order:''}">
  <form @submit.prevent="fetch('/user/payment', {method: 'POST', headers:{'Content-Type': 'application/json'},body: JSON.stringify(order)})">
    <div class="mt-6">
      <button><span>Proceed to RazorPay<br>₹ 747</span>
      </button>
    </div>
  </form>
</div>

<Br>
<Br>
<!-- I like something Like this so action page but here i like to send array of data -->
  <form method="POST" action= '/user/payment'>
  <input type="text" name="order"/>
    <div class="mt-6">
      <button><span>Proceed to RazorPay<br>₹ 747</span>
      </button>
    </div>
  </form>

【问题讨论】:

  • Fetch 遵循30X 重定向并返回上次重定向的数据。您是否也尝试重定向页面?'
  • 就像新页面一样 form action="" 但使用 javascript 发送数据而不是使用 html 表单
  • 好吧,如果你想让你的 fetch 表现得像一个真实的表单,为什么不直接使用一个真实的表单呢?顺便说一句,对于真正的表单,您可以在 &lt;form&gt; 元素中使用属性 target="_blank" 在新选项卡中提交它。

标签: javascript html json forms


【解决方案1】:

使用fetch后,可以像window.location.replace = your_url这样改变窗口的url。

【讨论】:

  • 但我喜欢打开新页面,所有数据都发送到新页面。
  • 您的意思是您需要 (1) 使用 fetch 发送数据并获得响应,然后 (2) 将相同的数据发送到 URL 并强制页面重新加载?
  • 你可以有一个带有 action 属性的表单,并添加一个带有点击事件的按钮(不是提交按钮)。单击按钮时 -> 通过 javascript 发送表单数据(使用 Ajax Form Plugin 中的 ajaxSubmit 函数或 jQuery 序列化函数),然后使用 javascript 以编程方式执行常规表单提交(例如:document.getElementById('#formId').submit();
猜你喜欢
  • 2014-03-01
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
相关资源
最近更新 更多