document.forms[0].submit();
document.forms['exportServlet'].submit();
(1)document.forms:表示获取当前页面的所有表单
(2)document.forms[0]:表示获取当前页面的第一个表单
(3)document.forms['exportServlet']:表示获取当前页面的name="exportServlet"的表单
(4)submit()表示提交函数
场景:
在做支付宝支付接入的时候,通过alipayClient发起请求后,返回页面进行重定向到支付宝支付界面
代码:
发起请求后,向前端返回text/html格式,然后进行自动提交
发起请求后,返回result给前端,result 内容:
<form name="punchout_form" method="post"
action="https://openapi.alipaydev.com/gateway.do?charset=utf-8&method=alipay.trade.page.pay&sign=CAQbktYmKK5BH%2Bt6HumFty9eEt1w7vq1TcpcFOH3xkttV2Ux2zzr1UJhoFDXAFZ%2F6wX774GBRYR03XR%2BwnsOK7WzOfMizxV%2BaUpSA92zk9AaN5%2BlzcOIn6CpdL7xZHpPcRWnfjxnhOtR5D3lnXiREF%2FCMZyrrXSkxDcDHWPZ1CPjZ%2FVdI8Wp%2F%2F%2BnpX56YxsrZWFyOaxBK1eqDe7a%2B8mC0jGgZp8i%2BHJaUx88g0k%2BxR1et1RJkMI6ECHA8gBq3DXsVJonRy%2Ffmsf3vzVTdTcw1aTD%2FMP%2F6pEK5NVohpOBHBupWyz3uPpo99lZs%2B0BL3RXKFZcQ4aIe9lR6rOsy%2Fqvwg%3D%3D&return_url=http%3A%2F%2Fsecond.free.idcfengye.com%2FbillingPackageOrder%2FalipayReturnNotice¬ify_url=http%3A%2F%2Fsecond.free.idcfengye.com%2FbillingPackageOrder%2FalipayNotifyNotice&version=1.0&app_id=2021000117683029&sign_type=RSA2×tamp=2021-07-03+17%3A31%3A31&alipay_sdk=alipay-sdk-java-dynamicVersionNo&format=json">
<input type="hidden" name="biz_content"
value="{"out_trade_no":"1625304691429","total_amount":"10.0","subject":"正式1","body":"用户订购商品个数:1,客户:15","timeout_express":"1c","product_code":"FAST_INSTANT_TRADE_PAY"}">
<input type="submit" value="立即支付" style="display:none">
</form>
<script>document.forms[0].submit();</script>
页面通过<script>document.forms[0].submit();</script>进行自动提交