0

I am using XmlhttpRequest to send a POST requests on localhost and chrome developer tools console and network to verify the requests.

I have two methods: Method1 send the post requests. Requesthandler handles the requests response.

When the request is successful (200) it does not throw an error and the request handler outputs a message on console. But when the request is unsuccessful. My console throws an error despite the request handler.

For example, If I purposely set the wrong url adress. I expect a 404 status Not Found exception and the request handler to output a message on console. Despite it happening, it still goes ahead and throws on the error on Send() Method.

Why xmlhttprequest.send() throws errors despite handling responses on the onreadystatechange event?

enter image description here

Method1(Sends Requests)

enter image description here

RequestHandler(Handles responses)

enter image description here

Console Message:

enter image description here

Thrown Error:

enter image description here

sleepyJoe
  • 21
  • 2
  • 7
  • 2
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Ivar Dec 19 '21 at 17:18
  • Can you make sure that `http://localhost:5101/lolo` with POST method works? – Areg Dec 19 '21 at 17:18
  • 1
    Does this answer your question? [Check if file exists but prevent 404 error in console from showing up](https://stackoverflow.com/questions/7035466/check-if-file-exists-but-prevent-404-error-in-console-from-showing-up) – Ivar Dec 19 '21 at 17:20
  • There's a fundamental error in the code: `xj.onreadystatechange = requestHandler()`, you should not call the function meant to be an event handler (and when that function doesn't return a function), instead refer the function. – Teemu Dec 19 '21 at 17:41
  • 1
    @Ivar I did not know that you can not avoid getting a 404 error in the console on chrome despite attempting to error handle it. Thanks for the info – sleepyJoe Dec 22 '21 at 11:49

0 Answers0