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 are going to use AJAX (Asynchronus 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.<!DOCTYPE html><html><head><title>JavaScript | Sending JSON data to server.</title></head><bodystyle="text-align:center;"id="body"><h1style="color:green;">GeeksForGeeks</h1><p><!-- Making a text input --><inputtype="text"id="name"placeholder="Your name"><inputtype="email"id="email"placeholder="Email"><!-- Button to send data --><buttononclick="sendJSON()">Send JSON</button><!-- For printing result from server --><pclass="result"style="color:green"></p></p><!-- Include the JavaScript file --><scriptsrc="index.js"></script></body></html>chevron_rightfilter_none - 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.functionsendJSON(){let result = document.querySelector('.result');let name = document.querySelector('#name');let email = document.querySelector('#email');// Creating a XHR objectlet xhr =newXMLHttpRequest();let url ="submit.php";// open a connectionxhr.open("POST", url,true);// Set the request header i.e. which type of content you are sendingxhr.setRequestHeader("Content-Type","application/json");// Create a state change callbackxhr.onreadystatechange =function() {if(xhr.readyState === 4 && xhr.status === 200) {// Print received data from serverresult.innerHTML =this.responseText;}};// Converting JSON data to stringvardata = JSON.stringify({"name": name.value,"email": email.value });// Sending data with the requestxhr.send(data);}chevron_rightfilter_none - 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.
<?phpheader("Content-Type: application/json");echo"Hello $data->name, your email is $data->email";?>chevron_rightfilter_none
After running the code above you’ll see something like this –

Now when you fill the details and press the Send JSON button you’ll see something like:

Recommended Posts:
- JavaScript | How to add an element to a JSON object?
- Deserializing a JSON into a JavaScript object
- JavaScript | Add new attribute to JSON object
- Converting JSON text to JavaScript Object
- How to Convert JS Object to JSON String in JQuery/Javascript?
- JavaScript | Check if a key exists inside a JSON object
- JSON | modify an array value of a JSON object
- Send unlimited Whatsapp messages using JavaScript
- Convert Json String to Java Object Using GSON
- Convert Java Object to Json String using Jackson API
- Convert Java Object to Json String using GSON
- Python | Ways to convert string to json object
- JavaScript JSON
- Javascript | JSON PHP
- JavaScript | JSON HTML
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



