Send a POST Request

PHOTO EMBED

Sun Oct 18 2020 17:04:11 GMT+0000 (Coordinated Universal Time)

Saved by @aymanelya #javascript

const httpPost = (url, data, callback, err = console.error) => {
  const request = new XMLHttpRequest();
  request.open('POST', url, true);
  request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
  request.onload = () => callback(request.responseText);
  request.onerror = () => err(request);
  request.send(data);
};

const newPost = {
  userId: 1,
  id: 1337,
  title: 'Foo',
  body: 'bar bar bar'
};
const data = JSON.stringify(newPost);
httpPost(
  'https://jsonplaceholder.typicode.com/posts',
  data,
  console.log
); 

// Logs: {"userId": 1, "id": 1337, "title": "Foo", "body": "bar bar bar"}
content_copyCOPY

https://blog.madza.dev/24-modern-es6-code-snippets-to-solve-practical-js-problems?guid