0

I'm trying to use the below snippet to capture the HTTP response of the form request and display the response in the browser. But clicking for Submit button navigates me away from my website.

<!DOCTYPE html>
<html lang="en">
   <head>
   </head>
   <body>
    <form name="responseHere" action="https://www.stackoverflow.com/">
      <input type="submit" value="Submit request" />
    </form>
      <script type="text/javascript">
            var responseHere = document.getElementById("responseHere").value;
            alert(responseHere)
      </script>
   </body>
</html>

I've also tried XHR methods to perform GET requests, but receive a CORS error when attempting to query domains outside my website's domain.

Is it possible to capture the HTTP response from the form submission and pass it to alert() or console.log() in Javascript?

zihobu
  • 37
  • 3
  • 1
    Does this answer your question? [How can I listen to the form submit event in javascript?](https://stackoverflow.com/questions/7410063/how-can-i-listen-to-the-form-submit-event-in-javascript) – computercarguy Oct 11 '21 at 21:34
  • Thanks, @computercarguy. Would you be able to help formulate a solution using one of the solutions in that link? I'm having trouble putting that into context. – zihobu Oct 11 '21 at 21:40
  • Just follow the 2nd code block of that first answer. Replace "myForm" in the `querySelector` call with "responseHere", remove your `action`, and put in your code after the `if` statement ends. It's been a while since I did this, but it should work. You might have to tweak it a little, though. You should have all the form data in the "responseHere" element. – computercarguy Oct 11 '21 at 21:56

0 Answers0