使用ajax处理表单提交信息

很直观的需求

1、需要在不进行页面跳转的情况下进行表单验证,或者只是单纯的数据上传

2、不使用默认的提交方法,很low!和难受

这是表单

<form id="form">
  <input type="text" name="user" id="user" placeholder="user">
  <br>
  <input type="password" name="password" id="password" placeholder="password">
  <br>
  <input type="file" name="file" id="file">
  <br>
  <!-- 自带回调函数的,就是会get提交 -->
  <input type="submit" name="submit" id="submit">
  <!-- 自带回调函数的,就是会重置 -->
  <input type="reset" name="reset" id="reset">
</form>

这是脚本

<script type="text/javascript">

  var submit = document.getElementById(‘submit‘);
  submit.addEventListener(‘click‘,(e)=>{
    //这样就能阻止默认的提交事件了
    e.preventDefault();
    // var user = document.getElementById(‘user‘);
    // var password = document.getElementById(‘password‘);
    // var file = document.getElementById(‘file‘);
    // // 获取所有的数据
    // console.log(user.value);
    // console.log(password.value);
    // console.log(file.value);

    //单纯获取value是没有用的,比如只能获取文件名
    var form = document.getElementById(‘form‘);
    var formData = new FormData(form);
    console.log(formData.get(‘user‘));
    console.log(formData.get(‘password‘));
    console.log(formData.get(‘file‘));

    //使用ajax
    var ajax = new XMLHttpRequest();
    ajax.open("POST", "/api/xxxxx", true);
    ajax.send(formData);
    ajax.onreadystatechange = ()=>{
      //如果成功,进行跳转
      setTimeout(()=>{
        location.href = ‘https://www.baidu.com‘;
      },2000);
    };

  })

</script>

总结

1、阻止默认,prevent

2、封装表单数据,使用formdata!不然文件只获取value的话,只有名字。而file应该是一个对象

3、页面跳转location.href

原文地址:https://www.cnblogs.com/weizhibin1996/p/9743768.html

时间: 2024-08-29 01:58:02

使用ajax处理表单提交信息的相关文章

ajax form表单提交 input file中的文件

ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了解决这个问题我走了不少弯路: 1.用原生的 input file , 不支持ajax上传文件,你肯定会说可以用 ajax form表单上传了呀?不过我后面还要调用上传成功后用js处理一些对话框,所以这种方法排除 2.用了 uploadify 上传插件,弄出来能上传东西,结果不理想:因为不能判断上传的

ajax的表单提交,与传送数据

ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: "json", method : 'post', data: $("#form_portDetect").serialize(),              //传送表单数据 success : function(data_) { //alert(data_); ale

Linux curl 模拟form表单提交信息和文件

curl是一个命令行方式下传输数据的开源传输工具,支持多种协议:FTP.HTTP.HTTPS.IMAP.POP3.TELNET等,功能超级强大. 我今天想说的是程序开发中常用的模拟Form提交 1.GET提交 特别简单直接写url里面 2.POST提交    通过 --data/-d 方式指定使用POST方式传递数据 3.模拟form表单提交文件  --form/-F 模拟form表单提交文件 这个命令超级好用,再也不用为了写上传接口,而被迫写一个Form表单了 "[email protecte

mvc Html.BeginForm和Ajax.BeginFrom表单提交

今天使用异步提交附件后台死活获取不到文件,代码还原 1 @using (Ajax.BeginForm("Add", "Event", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "EntryFormJS.SubmitSuccess" }, new { id = "myform" })) 2 { 3 <div class="form

Ajax form表单提交

1. 使用 $("form").serialize() 来获取表单数据 $.ajax({ type: 'post', url: 'your url', data: $("form").serialize(), success: function(data) { // your code } }); 2. 使用jquery.form.js 插件 $('#fm').form('submit', { url: url, onSubmit: function () { re

ajax模拟表单提交,后台使用npoi实现导入操作 方式一

页面代码: <form id="form1" enctype="multipart/form-data"> <div style="float:right">   <button type="button" class="btn btn-primary" onclick="$('#fileUpload').click()" id="reviewFi

4-16 表单提交信息和获取。

login.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+re

jeecg &lt;t:ckeditor ajax form表单提交 获取不到值问题解决

<t:ckeditor是jeecg里面的标签,要使用必须<t:formvalid里面使用 但是这次是要写独立的页面,跟之前的不一样,所以标签里面的内容获取不到,查看框架源代码 </textarea><script type="text/javascript">var ckeditor_goodsDetails=CKEDITOR.replace("goodsDetails_text",{filebrowserBrowseUrl:'p

form表单提交信息(包含文字以及照片信息)使用MultipartRequest进行读取

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setHeader("A