fetch的使用非常普遍,但是它不会用数据加载新页面,它只是发送数据和接收数据。
如何像普通HTML表单提交一样使用它,但是不用名称,我可以像表单操作那样发送数据,比如获取和打开新链接。打开新页。
如果我使用form动作,我必须使用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>
发布于 2021-12-25 19:39:23
使用fetch之后,您可以像下面的window.location.replace = your_url一样更改窗口的url。
https://stackoverflow.com/questions/70482280
复制相似问题