1

I am receiving json form when submitting a contact form , and I would like to get the inline message of it ;

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     
            }
           }

this the message when it's 200 :

{"inlineMessage":"Thanks for submitting the form."}

I have already tried to get the value of the inlineMessage like this :

xhr.responseText.inlineMessage 

but it didn't work ! any idea how to get that message

marcobiedermann
  • 4,317
  • 3
  • 24
  • 37
Jaacoubi
  • 97
  • 6

1 Answers1

1

this xhr.responseText.inlineMessage is not working because you're treating the response data as an object , while it is JSON string

So you can parse that JSON string with a JavaScript program and access the data as an object. you might do something like this :

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.
            } 
monim
  • 3,641
  • 2
  • 10
  • 25