Home > Web Front-end > JS Tutorial > body text

How to submit form data asynchronously using jquery's ajax

php中世界最好的语言
Release: 2017-12-31 11:11:21
Original
1501 people have browsed it

What I bring to you this time is how to use jquery's ajax to submit form data asynchronously. You can use jquery's ajax method to submit the form asynchronously. After success, the background returns json data, Callback function Processing, you can achieve asynchronous purposes without refreshing the page. This article will give you a good analysis.

The data processed in the form can be serialized using the serialize() method. If the submitted data includes a file stream, you need to use the FormData object:

Without Use the form data of the file: var data = $(form).serialize();

html:form form

<form id="addForm" action="${pageContext.request.contextPath}/admin/saveAdd" method="post">    
  <input type="text" name="name" placeholder="请输入名字" />
  <input type="password" name="password" placeholder="密码"/>
 </form>
Copy after login

jquery asynchronous processing



$("#submitAdd").click(function(){
     
  var targetUrl = $("#addForm").attr("action");    
  var data = $("#addForm").serialize();     
   $.ajax({ 
    type:&#39;post&#39;,  
    url:targetUrl, 
    cache: false,
    data:data,  
    dataType:&#39;json&#39;, 
    success:function(data){      
      alert(&#39;success&#39;);
    },
    error:function(){ 
     alert("请求失败")
    }
   })
     
})
Copy after login


I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to php Chinese website Other related articles !

Related reading:

Use jQuery to deduplicate and sort arrays

How can it be done with JS Click to jump to the logged-in personal email

There are several ways to implement chain operations in PHP

The above is the detailed content of How to submit form data asynchronously using jquery's ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template