Search code examples
javascripthttp-error

How to return an HTTP response in Javascript?


I have a pop-up banner that looks something like this:

if (confirm("Banner message")) {
  //do something if they accept
}
else {
  //do something else if they don't accept
}

If the user does not accept, I want to return a 403 Forbidden, preferably in a way that will get picked up by my httpd logs. What is the simplest way to do this?


Solution

  • confirm is an extension to JavaScript provided to JS by browsers that runs, client-side, in a webpage.

    You cannot (directly) send an HTTP response in reaction to it, because it is running in an HTTP client.

    If you want to log something in your server logs, then you need to make an HTTP request to your server.

    This would typically be done using the fetch or XMLHttpRequest APIs. If you want a standard 403 Forbidden error to be displayed to the user, then you can assign a URL to location that your server will return a 403 for instead.