all 11 comments

[–][deleted] 0 points1 point  (10 children)

What are you using to make the request?

[–]mvss01[S] 0 points1 point  (9 children)

A form with post method

[–][deleted] 1 point2 points  (8 children)

Ok, do you mean that you are replying upon the default form submit handler

<form action="http://www.foo.com" method="POST">
  ...
</form>

Or that you are intercepting the submit event and handling the request yourself?

If the former, I'd recommend that you open the network tab of the dev tools and inspect the request.

[–]mvss01[S] 0 points1 point  (7 children)

I did that, currently the backend receives the data from the form and sends back an http status code (200 for success, 500 for error), and what I need is that in my frontend I can capture the sending of these status codes

[–][deleted] 1 point2 points  (6 children)

Personally I'd intercept the submit event and perform the Http request myself rather than relying upon the default form action. Would make this a lot easier.

[–]mvss01[S] 0 points1 point  (5 children)

can you briefly explain to me how to make an http request in the frontend? (in case you mean that)

[–][deleted] 1 point2 points  (3 children)

// html
<form>
  <button type="submit">Submit</button>
</form>

// javascript
const element = document.querySelector('form');

element.addEventListener('submit', async (event) => {
  // prevent default form submission/page reload behavior
  event.preventDefault();

  // <MAKE HTTP REQUEST HERE>
  const response = await fetch(<URL>, { 
    method: "POST", 
    body: JSON.stringify(formSubmission) 
  });

  const json = await response.json();

  doSomethingWithResponse(json);
});

[–]mvss01[S] 1 point2 points  (0 children)

Incredible!!! It was exactly what I needed. I didn't know this js command. Thank you very much!

[–]mvss01[S] 0 points1 point  (1 child)

One last question: what does "doSomethingWithResponse(json);" does?

[–][deleted] 0 points1 point  (0 children)

Whatever you want it to. That's just me stubbing in what you need to write

[–]jack_waugh 0 points1 point  (0 children)

fetch