Recibo un formulario json cuando envío un formulario de contacto, y me gustaría recibir el mensaje en línea;
xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { setFormMessage(xhr.responseText.) // Returns a 200 response if the submission is successful. } else if (xhr.readyState == 4 && xhr.status == 400){ setFormMessage(xhr.responseTex)); // Returns a 400 error the submission is rejected. } else if (xhr.readyState == 4 && xhr.status == 403){ setFormMessage(xhr.responseText); // Returns a 403 error if the portal isn't allowed to post submissions. } else if (xhr.readyState == 4 && xhr.status == 404){ setFormMessage(xhr.responseText); //Returns a 404 error if the formGuid isn't found } }este es el mensaje cuando es 200:
{"inlineMessage":"Thanks for submitting the form."}Ya he intentado obtener el valor de inlineMessage de esta manera:
xhr.responseText.inlineMessage¡pero no funcionó! alguna idea de como llegar a ese mensaje
este xhr.responseText.inlineMessage no funciona porque está tratando los datos de respuesta como un objeto, mientras que es una cadena JSON
Entonces puede analizar esa cadena JSON con un programa JavaScript y acceder a los datos como un objeto. podrías hacer algo como esto:
xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { const jsonResponse = JSON.parse(xhr.responseText) setFormMessage(jsonResponse.inlineMessage) // Returns a 200 response if the submission is successful. }