简体   繁体   中英

Is it OK to respond with a 400 Bad Request when the client does not use AJAX?

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, ie does not contain the X-Requested-With: XMLHttpRequest header.

Is 400 Bad Request suitable for this purpose?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM