Not sure if you want jQuery.

var form;

form.onsubmit = function (e) {
  // stop the regular form submission
  e.preventDefault();

  // collect the form data while iterating over the inputs
  var data = {};
  for (var i = 0, ii = form.length; i < ii; ++i) {
    var input = form[i];
    if (input.name) {
      data[input.name] = input.value;
    }
  }

  // construct an HTTP request
  var xhr = new XMLHttpRequest();
  xhr.open(form.method, form.action, true);
  xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

  // send the collected data as JSON
  xhr.send(JSON.stringify(data));

  xhr.onloadend = function () {
    // done
  };
};
Answer from J. K. on Stack Overflow
๐ŸŒ
JSON Editor Online
jsoneditoronline.org โ€บ home โ€บ data-fetching โ€บ post-json
How to POST JSON data in JavaScript | Indepth | JSON Editor Online
February 8, 2023 - This article explains what HTTP requests are and how to POST JSON data in JavaScript. Learn about common pitfalls and handy tools to fetch JSON data.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ API โ€บ Request โ€บ json
Request: json() method - Web APIs | MDN
November 7, 2025 - ... The request body cannot be parsed as JSON. ... const obj = { hello: "world" }; const request = new Request("/myEndpoint", { method: "POST", body: JSON.stringify(obj), }); request.json().then((data) => { // do something with the data sent in the request });
Discussions

javascript - POST data in JSON format - Stack Overflow
I have some data that I need to convert to JSON format and then POST it with a JavaScript function. More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Using JSON POST Request - Stack Overflow
I'm attempting to use JSON to initiate a POST request to an API. I've found some example code, and before I get too far I wanted to get that working, but I'm stuck... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 21, 2015
POST Request with JSON Data in Javascript - Stack Overflow
I've only been sending Python requests ... JSON or querystring payloads. This is my first time sending requests with Javascript. I have to say that it does receive something. Webhook.site recognises that a request has been received, it just has no payload attached to the request. I'll add a screenshot to the original question to clear that up. ... Save this answer. ... Show activity on this post. In your example, the webhook.site ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Sending a JSON to server and retrieving a JSON in return, without JQuery - Stack Overflow
It shows how you can get/post data with vanilla JS. ... @Ed Cottrell The referenced question has nothing to do with this one. The reference is taking about (JUST) sending an ajax request, which is a quite general thing. This one is asking for sending but and receiving JSON in pure JavaScript. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ sending and receiving json data via post
JavaScript Tutorial => Sending and Receiving JSON Data via POST
Methods on the Response object ... to load, then parse it. const requestData = { method : 'getUsers' }; const usersPromise = fetch('/api', { method : 'POST', body : JSON.stringify(requestData) }).then(response => { if ...
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ wzp2hxwh โ€บ javascript-post-request-example
How do I send a POST request using JavaScript?
To receive notifications when the status of a request has changed, we need to subscribe to the onreadystatechange event. POST request headers can be added using the setRequestHeader method. Below is an example of submitting JSON to the ReqBin echo URL with XMLHttpRequest object. JavaScript POST ...
๐ŸŒ
DEV Community
dev.to โ€บ ldakanno โ€บ making-a-post-request-using-json-server-h7c
Making a POST request using json-server - DEV Community
December 11, 2022 - To make any request other than ... to use, in this example it would be POST since we are making a POST request. The header will communicate what kind of data we will be sending. The JSON.stringify method is really neat. It converts a javascript object into a JSON ...
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ xhr-json-post-request
How to send JSON request using XMLHttpRequest (XHR)
October 12, 2022 - const xhr = new XMLHttpRequest() ... request xhr.open('POST', 'https://reqres.in/api/login') // set `Content-Type` header xhr.setRequestHeader('Content-Type', 'application/json') // send rquest with JSON payload xhr.send(JSON.stringify(json))...
Find elsewhere
๐ŸŒ
ReqBin
reqbin.com โ€บ req โ€บ 4rwevrqh โ€บ post-json-example
How do I post JSON to the server?
January 16, 2023 - Click Send to execute the POST JSON example online and see the results. ... POST /echo/post/json HTTP/1.1 Host: reqbin.com Accept: application/json Content-Type: application/json Content-Length: 81 { "Id": 78912, "Customer": "Jason Sweet", "Quantity": 1, "Price": 18.00 } ... JavaScript Object Notation (JSON) is a textual, language-neutral data interchange format that defines a small set of formatting rules for the portable representation of structured data.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ node.js โ€บ how-to-post-json-data-to-server
How to Post JSON Data to Server ? - GeeksforGeeks
August 1, 2024 - The fetch API is a modern and versatile method to make HTTP requests in JavaScript. It is built into modern browsers and provides a simple interface for fetching resources across the network. Step 1: Create a folder application by using this command ... Step 3: Initialize the NodeJS application. ... Step 4: Install the necessary packages/libraries in your project using the following commands. ... Example: Implementation to show post JSON data to the server by setting up the server.
๐ŸŒ
Apidog
apidog.com โ€บ blog โ€บ fetch-post-json
How to Fetch POST JSON Data
In this example, we are sending a POST request to https://example.com/api/data with a JSON payload containing a single key-value pair. The Content-Type header is set to application/json to indicate that the payload is in JSON format.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ basics-of-http-requests-with-javascript โ€บ lessons โ€บ sending-data-with-post-requests-using-javascript
Sending Data with POST Requests Using JavaScript
The data is structured as a JavaScript object: In JavaScript, you can use the Fetch API to send the request body by utilizing the fetch() method with async/await. Before sending the data, it needs to be converted into a JSON string using the JSON.stringify() method.
๐ŸŒ
Apidog
apidog.com โ€บ blog โ€บ jquery-post-json
How to Use jQuery to Post JSON Data?
February 9, 2026 - Learn how to use jQuery to post JSON data to an API with this comprehensive guide. Includes step-by-step instructions, best practices, and more.
๐ŸŒ
Medium
medium.com โ€บ @kirstyn.nichole โ€บ creating-a-form-json-post-request-in-javascript-with-react-bad63a5e45bc
Creating a Form/JSON POST Request in JavaScript with React | by Kirstyn Canull | Medium
March 5, 2024 - The next step is to set up your GET and POST requests, (make sure to update the variables to reflect the data from your JSON file) and add in your handleSubmit function. In this example, our handleSubmit function (which is triggered when the form is submitted) prevents the default form submission behavior, and sends a POST request with the user-entered info to the server in JSON format.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ using-fetch-api-to-post-json-data-in-javascript
Using Fetch API to POST JSON Data in JavaScript
September 12, 2023 - const url = 'https://api.example.com/data'; const data = { username: 'example' }; fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }) .then(response => response.json()) .then(data => console.log(data)) .catch((error) => { console.er...
๐ŸŒ
jQuery
api.jquery.com โ€บ jquery.post
jQuery.post() | jQuery API Documentation
November 18, 2021 - Post to the test.php page and get content which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>).
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ javascript-post-requests-send-data-variables
JavaScript POST Request: Send Data with Fetch API (Code Examples) | Index.dev
December 17, 2024 - javascript fetch('https://api.example.com/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'John', email: '[email protected]' }) }) .then(response => response.json()) .then(data => ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-send-a-json-object-to-a-server-using-javascript
How to send a JSON object to a server using Javascript? | GeeksforGeeks
January 17, 2022 - In this article we are going to learn how to change JSON String into an object in javascript, JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet.
Top answer
1 of 2
1

In your example, the webhook.site service you're attempting to connect to with your JavaScript isn't enabled (by default) with the proper CORS headers that modern browsers respect & enforce to improve user security. The developer console in your browser of choice should point this out to you; the error mine gave back was:

Access to XMLHttpRequest at 'https://webhook.site/5c7a5049-9c5e-4bf7-b1cf-0e05f6503bfa' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

As the error states, the requested resource doesn't respond with any valid Access-Control-Allow-Origin header, which will prevent the POST itself from being fully executed. In webhook.site, you can select the CORS Headers tickbox from the top of the user interface to enable the service to send the proper CORS headers to get this working.

Copylet xhr = new XMLHttpRequest();

xhr.open("POST","https://webhook.site/5c7a5049-9c5e-4bf7-b1cf-0e05f6503bfa",true); // configuration interface at https://webhook.site/#!/5c7a5049-9c5e-4bf7-b1cf-0e05f6503bfa/e14fc471-4bc4-410f-b16a-0755a231fb12/1

xhr.setRequestHeader("Content-Type", "application/json");

let data = JSON.stringify({'eventType' : 'test'});

xhr.send(data);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 2
-1

I suggest you to use axios, it gonna be something like this and you will get the response from your post request

Copyconst response = await axios.post('https://webhook.site/4530328b-fc68-404a-9427-3f2ccd853066/', {'eventType' : 'test'});
Top answer
1 of 2
290

Sending and receiving data in JSON format using POST method

// Sending and receiving data in JSON format using POST method
//
var xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var json = JSON.parse(xhr.responseText);
        console.log(json.email + ", " + json.password);
    }
};
var data = JSON.stringify({"email": "[email protected]", "password": "101010"});
xhr.send(data);

Sending and receiving data in JSON format using GET method

// Sending a receiving data in JSON format using GET method
//      
var xhr = new XMLHttpRequest();
var url = "url?data=" + encodeURIComponent(JSON.stringify({"email": "[email protected]", "password": "101010"}));
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var json = JSON.parse(xhr.responseText);
        console.log(json.email + ", " + json.password);
    }
};
xhr.send();

Handling data in JSON format on the server-side using PHP

<?php
// Handling data in JSON format on the server-side using PHP
//
header("Content-Type: application/json");
// build a PHP variable from JSON sent using POST method
$v = json_decode(stripslashes(file_get_contents("php://input")));
// build a PHP variable from JSON sent using GET method
$v = json_decode(stripslashes($_GET["data"]));
// encode the PHP variable to JSON and send it back on client-side
echo json_encode($v);
?>

The limit of the length of an HTTP Get request is dependent on both the server and the client (browser) used, from 2kB - 8kB. The server should return 414 (Request-URI Too Long) status if an URI is longer than the server can handle.

Note Someone said that I could use state names instead of state values; in other words I could use xhr.readyState === xhr.DONE instead of xhr.readyState === 4 The problem is that Internet Explorer uses different state names so it's better to use state values.

2 of 2
29

Using new api fetch:

const dataToSend = JSON.stringify({"email": "[email protected]", "password": "101010"});
let dataReceived = ""; 
fetch("", {
    credentials: "same-origin",
    mode: "same-origin",
    method: "post",
    headers: { "Content-Type": "application/json" },
    body: dataToSend
})
    .then(resp => {
        if (resp.status === 200) {
            return resp.json()
        } else {
            console.log("Status: " + resp.status)
            return Promise.reject("server")
        }
    })
    .then(dataJson => {
        dataReceived = JSON.parse(dataJson)
    })
    .catch(err => {
        if (err === "server") return
        console.log(err)
    })

console.log(`Received: ${dataReceived}`)                
You need to handle when server sends other status rather than 200(ok), you should reject that result because if you were to left it in blank, it will try to parse the json but there isn't, so it will throw an error