How do I set up an AJAX page?

For testing I made it a little bit simpler. On the ajax.php template I use this:

<?php
if(kirby()->request()->ajax()) {
    echo 'AJAX';
}
else {
	header::status('404');
}

On my page where I use the AJAX function, I use this:

<script>
function test() {
   $.ajax({
      url: 'ajax',
      success : function(response) {        
        console.log(response);
        }
   });
}

setInterval(test, 7000);
</script>

It works like expected. Awesome!