How to send a JSON object to a server using Javascript? Last Updated : 08 Sep, 2025 Comments Improve Suggest changes Like Article Like Report 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 are going to use AJAX (Asynchronous JavaScript And XML), to send data in background. We are using PHP for the backend.Frontend:HTML: In the frontend we are going to build a form which takes name and email as a input and converts it into JSON object using javascript and send it to the server. After clicking the submit button a sendJSON() is called which is defined below. html <!DOCTYPE html> <html> <head> <title> JavaScript | Sending JSON data to server. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> <!-- Making a text input --> <input type="text" id="name" placeholder="Your name"> <input type="email" id="email" placeholder="Email"> <!-- Button to send data --> <button onclick="sendJSON()">Send JSON</button> <!-- For printing result from server --> <p class="result" style="color:green"></p> </p> <!-- Include the JavaScript file --> <script src="index.js"></script> </body> </html> JavaScript: When sending data to a web server, the data has to be a string. So we are using JSON.stringify() function to convert data to string and send it via XHR request to the server. Below is the sample code. javascript function sendJSON(){ let result = document.querySelector('.result'); let name = document.querySelector('#name'); let email = document.querySelector('#email'); // Creating a XHR object let xhr = new XMLHttpRequest(); let url = "submit.php"; // open a connection xhr.open("POST", url, true); // Set the request header i.e. which type of content you are sending xhr.setRequestHeader("Content-Type", "application/json"); // Create a state change callback xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { // Print received data from server result.innerHTML = this.responseText; } }; // Converting JSON data to string var data = JSON.stringify({ "name": name.value, "email": email.value }); // Sending data with the request xhr.send(data); } Backend: We are using PHP as a scripting language. Create a file named submit.php, in this file, we'll decode the received data to JSON and return a sentence formed using the received data. php <?php header("Content-Type: application/json"); $data = json_decode(file_get_contents("php://input")); echo "Hello $data->name, your email is $data->email"; ?> Now when you fill the details and press the Send JSON button you'll see something like: Comment More info F frikishaan Follow Improve Article Tags : JavaScript Web Technologies JSON Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like