1059 | | '''TODO''' |
| 1059 | [http://www.adaptivepath.com/publications/essays/archives/000385.php AJAX] (Asynchronous Javascript And XML) is all the rage today, and in many ways, it is indeed a helpful technique for improving the usability and responsiveness of web-based applications. |
| 1060 | |
| 1061 | To demonstrate how you'd use Genshi in a project that uses AJAX, let's enhance the Geddit commenting feature to use AJAX. We'll implement this in such a way that the current way comments work remains available, to serve those who don't have Javascript available, and also just to be good web citizens. That approach to using funky new techniques is often referred to as “unobtrusive Javascript”, and what it provides is “graceful degradation.” |
| 1062 | |
| 1063 | '''Note''': technically, what we'll be doing here isn't AJAX in the literal sense, because we'll not being transmitting XML. Instead, we'll respond with simple HTML fragment, a technique that is sometimes referred to as “AJAH”. |
| 1064 | |
| 1065 | Let's start by adding a new small module to our `lib` package. Inside the `geddit/lib` directory, create a file named `ajax`, and add the following code to it: |
| 1066 | |
| 1067 | {{{ |
| 1068 | #!python |
| 1069 | import cherrypy |
| 1070 | |
| 1071 | def is_xhr(): |
| 1072 | requested_with = cherrypy.request.headers.get('X-Requested-With') |
| 1073 | return requested_with and requested_with.lower() == 'xmlhttprequest' |
| 1074 | }}} |
| 1075 | |
| 1076 | This checks whether the current request originates from usage of AJAX (technically, the `XMLHttpRequest` Javascript object), based on a convention commonly used in Javascript libraries to add the special HTTP header “`X-Requested-With: XMLHttpRequest`” to all requests. |