HOWTO: Configure Apache to Return a HTTP 204 (No Content) for AJAX

| No TrackBacks
apache-gen-http-204-small.pngWhen dealing with AJAX, you might need to configure Apache to return a HTTP 204 (No Content).  This is useful when your AJAX scripts need to "ping" the server, but you don't want the server to actually return any data in the response (e.g., just acknowledge the request).  The server might do something behind the scenes though (like log the request) before it returns a 204.  As I understand it, the only difference between a 200 and a 204, is that a 204 response means that "the server has fulfilled the request but does not need to return an entity-body".

I tried to figure out how to configure Apache to return a 204 No Content using one of the built in modules, like mod_actions, mod_alias, or mod_headersSurprisingly, there does not appear to be a module that can intercept a request and blindly return a response of your choice.  The best solution seems to involve using a small a Perl/CGI script.  So, I hacked some things together to get this to work. Andrew Grangaard alerted me that you can configure RedirectMatch to return an HTTP 204 No Content with mod_alias.

Solution #1: Use RedirectMatch

As explained by Andrew Grangaard, you can use Apache's RedirectMatch directive to trigger on a specific URL pattern and reply with an HTTP 204 No Content response:

RedirectMatch 204 tracker(.*)$ 

This triggers on any URI starting with "tracker".


Solution #2: Use a Perl Script

You could also use Apache's SetHandler and Action directives to intercept the request and then call a Perl/CGI script to return a 204 No Content.  In my kolich.com VirtualHost configuration, I used the Location directive to define the URI I wanted Apache to trigger on:

<Location /tracker>
SetHandler nocontent-handler
Action nocontent-handler /gen_204.cgi virtual
</Location>

So this tells Apache to call gen_204.cgi each time a request comes starting with /tracker.  For example, if you visit http://mark.koli.ch/tracker/heydude then your browser will get a HTTP 204 response back.  Note the "virtual" modifier at the end of the Action directive.  Defining the action as "virtual" is important because it tells Apache not to check if the requested file/resource actually exists.  In the example above, /tracker/heydude doesn't actually exist (it's not a real resource on the server), so if you don't use the "virtual" modifier, Apache will return a 404 Not Found instead of calling the gen_204.cgi script.

Here's the gen_204.cgi Perl/CGI script I'm using that returns a 204 No Content.  This is called by Apache anytime a request comes in starting with /tracker:

#!/usr/bin/perl -w

use strict;
use warnings;
use CGI;

my $cgi = CGI->new();
print $cgi->header('text/html','204 No Content');

exit;

This seems to work very nicely.


Mark, why are you doing this?

I configured my blog's core JavaScript to "ping" kolich.com every time someone uses my Translate, Twitter, or Sharing widgets.  In other words, each time someone clicks a link in these widgets, my core JavaScript sends an AJAX request to the server to log the click.  This is so I can run reports against my Apache logs to see how many times folks actually used these widgets.

FWIW, Google actually does something similar.  Each time a user clicks on a link in their search results, Google pings itself to record the click; the response code from the ping is an HTTP 204.  Here's a screenshot showing the Google ping.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on March 22, 2009 10:52 AM.

HOWTO: Generate Your Own Self-Signed SSL Certificates (for HTTPS Apache) was the previous entry in this blog.

Picking the Right Battery Backup (UPS): APC BR1500LCD? is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.