How to pass multiple JSON Objects as data using jQuery's $.ajax() ? Last Updated : 30 Apr, 2021 Comments Improve Suggest changes Like Article Like Report The purpose of this article is to pass multiple JSON objects as data using the jQuery $ajax() method in an HTML document. Approach: Create a button in an HTML document to send JSON objects to a PHP server. In the JavaScript file, add a click event listener to the button. On clicking of the button, a request is made to PHP file using jQuery $ajax() method by which multiple JSON objects are passed to the server. HTML Code: The following code demonstrates the design or structure of the user interface. On click of the HTML button, it gives the response by the PHP server in the resultID HTML div. index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- CSS file --> <link rel="stylesheet" href="style.css"> <!-- jQuery Ajax CDN --> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"> </script> <!-- JavaScript file --> <script src="script.js"></script> </head> <body> <center> <h2 style="color:green">GeeksforGeeks</h2> <div class="container"> <b>Pass multiple JSON objects</b> <br/><br/> <!-- Button to multiple JSON objects --> <button type="button" id="btn"> Click on me! </button> <div style="height:10px"></div> <div id="resultID"></div> </div> </center> </body> </html> CSS Code: The following code is the content for the file “style.css” used in the above HTML code. Stylecss .container { border: 1px solid rgb(73, 72, 72); border-radius: 10px; margin: auto; padding: 10px; text-align: center; } button { border-radius: 5px; padding: 10px; color: #fff; background-color: #167deb; border-color: #0062cc; font-weight: bolder; cursor: pointer; } button:hover { text-decoration: none; background-color: #0069d9; border-color: #0062cc; } JavaScript Code: The following code is the content for the file “script.js” used in the above HTML code. It handles the click() event for the button by using jQuery ajax() method and passing the data to a PHP server file i.e action.php script.js $(document).ready(() => { // Adding 'click' event listener to button $("#btn").click(() => { // Two JSON objects are passed to server let obj1 = {"name": "John Doe"}; let obj2 = {"name": "Duke"}; // jQuery Ajax Post Request using $.ajax() $.ajax({ url: 'action.php', type: 'POST', // passing JSON objects as comma(,) separated values data: { obj1, obj2 }, success: (response) => { // response from PHP back-end PHP server $("#resultID").show().html(response); } }) }); }); Note: You can pass as many JSON objects by using comma(,) separated values i.e. obj1, obj2, obj3,.. PHP code: The following is the code for the file “action.php” used in the above JavaScript code. PHP <?php // Check if JSON Objects are set by user if (isset($_POST['obj1']) && $_POST['obj2']) { // Get JSON objects in variables $obj1 = $_POST['obj1']; $obj2 = $_POST['obj2']; // Sending response to script file echo "Success"; } ?> Output : multiple data passing and getting response Comment More infoAdvertise with us Next Article How to pass multiple JSON Objects as data using jQuery's $.ajax() ? asmitsirohi Follow Improve Article Tags : Web Technologies JQuery JSON jQuery-AJAX jQuery-Questions +1 More Similar Reads How to make a JSON call using jQuery ? Use the getJSON() function in jQuery to load JSON data. The getJSON() function uses a GET HTTP request to retrieve JSON-encoded data from the server. In this article, we will learn about the jQuery getJSON() function and its implementation through examples. Syntax: $(selector).getJSON(url, data, suc 3 min read How to Send FormData Objects with Ajax-requests in jQuery ? In this article, we will see how can we send formData objects with Ajax requests by using jQuery. To send formData, we use two methods, namely, the FormData() method and the second method is serialize() method. The implementation of Ajax to send form data enables the submission of data to the server 4 min read How to select values from a JSON object using jQuery ? In this article, we will select values from a JSON object and display them on the browser using jQuery. To select values from a JSON object to webpage, we use append() method. This append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).appen 2 min read How to get server response from an AJAX request using jQuery ? In this article, we will see how we can use jQuery to get the server response to an AJAX request. The jQuery ajax() method implements the basic Ajax functionality in jQuery. It communicates with the server via asynchronous HTTP requests. Syntax:$.ajax(url);$.ajax(url,[options]);Parameters:url: A URL 4 min read How to create clone of any object using jQuery ? In this article, we will learn to create a clone of an object using jQuery. This can be achieved using the extend() method of jQuery. The extend() method is used to merge the contents of multiple objects into the object passed as the first parameter. This can be used to clone an array as we can pass 3 min read How to add nested object to existing JSON using Postman ? Postman has become a critical tool for developers around the world, particularly when dealing with JSON data. This article will guide you through the process of adding nested objects to an existing JSON using Postman, with a focus on the approaches and syntax involved. Table of Content Understanding 4 min read How to send a JSON object to a server using Javascript? JavaScript Object Notation (JSON). It is a lightweight data transferring format. It is very easy to understand by human as well as machine. It is commonly used to send data from or to server. Nowadays it is widely used in API integration because of its advantages and simplicity.In this example we ar 2 min read How to convert JSON data to a html table using JavaScript/jQuery ? To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works.Table of ContentUsing for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a v 4 min read How to Pass JSON Data in a URL using CURL in PHP ? In this article, we will see how to pass the JSON Data in a URL using CURL in PHP, along with understanding the different ways for passing the data in a URL through the illustrations. The cURL stands for client URL, which allows us to connect with other URLs & use their responses in our code, i. 7 min read Handling nested data objects using jQuery DataTables plugin DataTables are a modern jQuery plugin for adding interactive and advanced controls to HTML tables for our webpage. It is a simple-to-use plug-in with many options for the developer's custom changes. The common features of DataTables are sorting, ordering, searching, and pagination.DataTables can eas 3 min read Like