I have to send the data entered into the contact form on the site, in JSON format (the request is allowed on the CORS server) when sending, I get Status 415

var inpName = document.querySelector('#name'); var inpPhone = document.querySelector('#phone'); var inpEmail = document.querySelector('#email'); var inpMessage = document.querySelector('#message'); function ajax(params) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { console.log('readyState: ' + xhr.readyState + ' status: ' + xhr.status); } } xhr.open('POST', 'http://localhost:8080/sendmail'); xhr.responseType = 'json'; xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(params); } var formSend = document.querySelector('input[type="button"]'); formSend.addEventListener('click', function() { var params = "name=" + inpName.value + "&" + "phone=" + inpPhone.value + "&" + "email=" + inpEmail.value + "&" + "message=" + inpMessage.value; ajax(params); }); 

  • What does CORS have to do with it? - Roman C
  • To convert to json is for this JSON.stringify() . - And

1 answer 1

Look at this piece of code , the transformation of a form into an object, below there is an inverse function - an object into a form. Perhaps it will help, there are examples of using and sending the object to the server in Json format.