form表单跨域提交在不使用jq的情况下能实现不跳转页面还能进行回调吗??

关键词:form跨域提交,不使用jq,不跳转不刷新,拿到返回值

我现在要使用form表单来实现跨域提交,但是代码中没有引入jq(也不准备引入,jq中的ajaxForm和ajaxSubmit的方法是可以实现的),请问我怎么通过js来模拟这两个方法,或者有其他更好的方法来实现(现在后台改不了)

<form id="form" action="http://*/api/gateway" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="upload_file">
<input type="hidden" name="appKey" value="appKey">
<input type="hidden" name="pampasCall" value="pampasCall">
<input type="hidden" name="pathName" value="pathName">
<input type="hidden" name="sign" value="signs">
<input type="hidden" name="timestamp" value="timestamp">
<button type="submit">提交</button>
</form>

阅读 2.1k
2 个回答

你要是用原生的form提交,肯定是要跳转的
你要么自己直接用xmlhttprequest/fetch创建请求(把form提交事件屏蔽),要么自己写个ajax什么的

<form id="form" action="http://*/api/gateway" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="upload_file">
<input type="hidden" name="appKey" value="appKey">
<input type="hidden" name="pampasCall" value="pampasCall">
<input type="hidden" name="pathName" value="pathName">
<input type="hidden" name="sign" value="signs">
<input type="hidden" name="timestamp" value="timestamp">
<button onclick="submit()">提交</button>
</form>
function submit(){
   $('#form').submit();
  return false;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题