I have already committed the general support ajax scripts in MDL-21938, it is enough to put AJAX_SCRIPT define before require(config.php) - it strips some OUTPUT functions, detects redirects and encodes all exceptions using json.
Going to commit changes so that we have one example, other places should be converted later.