1

A user tries to navigate away from a page, either by following a link, or submitting a form.

I want to do some server-side validation on HTTP requests before I allow them to proceed. If validation fails, I want the user to stay on the same page and get some notification.

Can the server generate a response that will make the user stay on the same page?

For example, I was thinking to use a Content-Disposition: bogus header to stay on the page (and a hack to get the validation failure), but apparently browsers try to save those responses to a file, as if the were attachment, instead of ignoring them.

I can inject javascript into the web pages, but the page logic is a black box for me (I can't write client-side code that's specific to the logic). So, I can't reimplement forms and links on the page as AJAX calls.

korn
  • 63
  • 4
  • You either have to send the request via ajax then decide what to do (navigate away from the page or stay on the page) or you need to navigate away from the page validate and then redirect the user back to the page or to where they wanted to go. – Get Off My Lawn Aug 24 '19 at 00:51
  • @GetOffMyLawn I'm looking for out-of-the-box solutions :) – korn Aug 24 '19 at 00:56
  • Those are out of the box solutions. – Get Off My Lawn Aug 24 '19 at 01:05
  • I meant, thinking outside the box. Those are pretty obvious solutions, but unfortunately, the first one doesn't seem doable in an automated way, and the second one will result in poor user experience. – korn Aug 24 '19 at 01:07
  • Then you will need to use sockets, otherwise there is no other solution to access the server. – Get Off My Lawn Aug 24 '19 at 01:10
  • 1
    A 204 or 205 response code should cancel the navigation, but there is no way to pass back a validation message as far as I am aware. – kicken Aug 24 '19 at 05:51
  • @kicken Thank you, 204 is what I was looking for! Change it to an answer, I'll accept. – korn Aug 26 '19 at 07:39

0 Answers0