I agree with ZAP that there’s no benefit in arguing over the pros and cons and appropriateness of using AJAX calls on web pages. It all depends on your perspective. So what if snop is actually building an application where the use of Ajax is in fact helpful to the user and the solution he is working towards is to provide a convenient fall-back solution for those that do not have javascript enabled? I see nothing wrong with that.
As far as a solution is concerned I see some merit in the "isXmlHttpRequest" function but instead of hacking the core I think it’s quite possible to do this with a simple plugin using the onWebPageInit event. I played around with it just now and with a plugin and one single extra page for the ajax calls and using a slightly adapted version of the getField snippet I was able to achieve something usable.
<?php
//set onWebPageInit system event
$ajax_page = 108; //id of page handling ajax calls
$e = & $modx->Event;
if( $e->name=='OnWebPageInit' )
{
$id = $modx->documentIdentifier;
if( isXmlHttpRequest() ){
$_SESSION['doc_id'] = $id;
$modx->sendForward($ajax_page);
}
}
if(!function_exists('getHeader')){
function getHeader($header) {
if (empty($header)) {
;
}
/* Pokusiť sa ich najpr získať z globálneho poľa $_SERVER. */
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (!empty($_SERVER[$temp])) {
return $_SERVER[$temp];
}
/* Toto vyzerá ako jediné riešenie ako získať Authorization header na Apache serveri. */
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (!empty($headers[$header])) {
return $headers[$header];
}
}
return false;
}
}
if(!function_exists('isXmlHttpRequest')){
function isXmlHttpRequest()
{
return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
}
}
The ajax handling page only has this call in it’s content: I gave it an empty template but you could easily wrap some xml around it if you wanted. The page needs to be un-cached.
[[getContentField? &field=`content`]]
Where getContentField is a slightly modified getField snippet. The only change I made is a change to the &docid parameter: (approx. line 24)
$gfIntDocId = $_SESSION['doc_id']?$_SESSION['doc_id']:($docid ? $docid : $modx->documentIdentifier);
So if an XmlHttpRequest is made the request is forwarded to the ajax handler page which then uses the getContentField snippet to fetch the original content.