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); });
jsonis for thisJSON.stringify(). - And