Ad

Is It OK To Respond With A 400 Bad Request When The Client Does Not Use AJAX?

- 1 answer

I'm developing a web app that extensively uses modal dialogs.

The dialogs are loaded via AJAX, and the HTTP response contains just a portion of HTML, not a full HTML document. I don't want a search engine to index these publicly accessible URLs if it finds them.

I can't use a meta robots, as the HTML portion returned does not have a <head>. I prefer to avoid adding every single URL to my robots.txt, or grouping all my URLs under a common prefix, such as /ajax/.

So I thought I could simply return a 400 Bad Request if the request is not performed with AJAX, i.e. does not contain the X-Requested-With: XMLHttpRequest header.

Is 400 Bad Request suitable for this purpose?

Ad

Answer

Verbatim copy of RFC 7231, section 6.5.1:

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error

So yes, it is okay.

Ad
source: stackoverflow.com
Ad