Categories Photography Résumé About Me

Gracefully Handling AJAX Requests with ASP Classic

06 Jul 2011

I've been working with so-called "Classic" ASP for about two years now. Here's a trick I've put into practice with great success. Often when writing data-driven web scripts you may need to alter the presentation of that data based on the requester or some other variable. For instance, a script may be requested via a web browser which expects HTML output. However that same script may also be requested via an XMLHTTPRequest from within the browser which might expect XML or JSON formatting. (AJAX) The simplest way to detect if a request is from AJAX or not is to check for the X-Requested-With</code> header.

ASP provides you with the Request</code> object which has many properties and methods to provide you information about the current HTTP Request being handled. Because ISAPI follows the CGI standard for accessing HTTP request headers you can just access them the same way you would a normal request header. Just append HTTP_</code> to whatever request header you want to access. Here's the AJAX detection demonstration:

If Request.ServerVariables("HTTP_X-Requested-With") = "XMLHttpRequest" Then
  ' do some stuff (Freaking VBScript comments)'
Else %>


Stuff</h1> </body> </html> <% End If %></code></pre>

Filed under

  • Daniel 2011-09-30 04:02:31 -0400

    Thanks a lot, I have used it to determine when i make calls with ajax.

comments powered by Disqus