POST Request with JSON Body

Send a POST request with a JSON payload to a cross-origin server and include headers to specify the content-type.
// Define the data you want to send in the JSON payload
const data = {
    key: 'value',
    anotherKey: 'anotherValue'

// Create a configuration object for the POST request
const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data),
    mode: 'cors' // This will make the request cross-origin

// The URL to which you want to send the POST request
const url = '';

// Use the Fetch API to make the POST request
fetch(url, requestOptions)
    .then(response => response.json())
    .then(data => console.log('Success:', data))
    .catch((error) => console.error('Error:', error));
This code defines a JSON payload and sends it as the body of a POST request to a cross-origin server using the Fetch API. It sets the 'Content-Type' header to 'application/json' to inform the server of the payload's format. It also handles the promise returned by fetch() with a chain of .then() for success and .catch() for error.