- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to send FormData objects with Ajax-requests in jQuery?
In this tutorial, we will learn How to send FormData objects with Ajax requests in jQuery.
The FormData object stores value in the form of key-value pair. It mainly sends form data to the server with HTTP requests. If the form’s encoding type had been set to multipart/form-data, the submitted data would have been sent similarly via the submit() function. The FormData object contains multiple operations methods such as append, delete, set, etc.
Syntax
const formData = new FormData() formData.append('key', 'value')
Users can follow the above syntax to create a FormData object.
Asynchronous JavaScript And XML are referred to as AJAX. It does asynchronous activities on your web page. AJAX uses HTTP requests to communicate with the server and receives the information requested as an HTTP response. The jQuery library has methods to perform ajax operations efficiently. We can send the FormData object in the data parameter of the ajax.
Syntax
//FormData Object const formData = new FormData() formData.append('key', 'value') // AJAX $.ajax({ url: '...URL ENDPOINT', data: formData, type: 'POST', // GET/POST processData: false, success: function (data) { // Code blocks if formData send successfully }, error: function (err) { // Code blocks if formData send failed }, })
In the above syntax, we showed how we could send the FormData object with Ajax requests in jQuery.
Example
In the below example, we have used the Ajax requests in jQuery to send the FormData object to the backend server. We have taken multiple input fields to take user input for name, email, and password, and these input field values will be bundled together in the FormData object and will send to the backend server. The ajax method of jQuery helps to send the HTTP request. In the data parameter of ajax, we set the FormData object. The method is set to POST to send a post request. A button click event handler is associated with the ‘Send’ button that performs the ajax request to the server.
<html> <head> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <h2> Sending <i> FormData objects with Ajax requests </i> in jQuery </h2> <label for = "name">Name: </label> <input id = "name" type = "text" name = "name" /><br> <label for = "email">Email: </label> <input id = "email" type = "text" name = "email" /><br><br> <button id = "btn" onclick = "btnClick()">Send</button> <div id = "root" >Click on the send button to submit the FormData object</div> <script> const root = document.getElementById('root') const name = document.getElementById('name') const email = document.getElementById('email') function btnClick() { //FormData Object const formData = new FormData() formData.append('name', name.value) formData.append('email', email.value) // AJAX $.ajax({ url: 'https://jsonplaceholder.typicode.com/posts', data: formData, type: 'POST', processData: false, contentType: false, success: function (data) { root.innerHTML = 'FormData Object Send Successfully!' }, error: function (err) { root.innerHTML = 'FormData Object Send Failed!' }, }) } </script> </body> </html>
This tutorial taught us how to send FormData objects with Ajax requests in jQuery. In addition, we learned about the FormData object, the Ajax requests, and their syntax. In the example, we learned how to send input field data as a FormData object to the server using Ajax requests. Users can follow the example to understand the FormData object sent with Ajax requests in jQuery.
- Related Articles
- How to use POST method to send data in jQuery Ajax?
- How to use GET method to send data in jQuery Ajax?
- How to register a handler to be called when all Ajax requests completed using jQuery?
- How to register a handler to be called when all Ajax requests completed using jQuery?
- Send multiple data with ajax in PHP
- How to handle jQuery AJAX error?
- How to handle jQuery AJAX success event?
- What is the difference between Ajax and jQuery-Ajax methods in jQuery?
- How to send button value to PHP backend via POST using ajax?
- Using HTML5 file uploads with AJAX and jQuery
- Get requests using AJAX by making a Custom HTTP library
- What are jQuery AJAX Events?
- How to bind jQuery events within dynamic content returned via Ajax?
- How to retrieve data from JSON file using jQuery and Ajax?
- How to disable some jQuery global Ajax event handlers for a request?
