I've noticed a couple of scripts recently which aim to render both HTML, and JSON in one script rather than splitting out the HTML to one script, and JSON to another.
So far I think that they've all (I think there's a whole two) taken the 'ajax' parameter. If it's set then they return json instead of html.
However, because AJAX_SCRIPT has to be defined before config.php is included and the page is setup, any exceptions which may be thrown would not be wrapped in JSON but use the standard HTML output.
There are two possible solutions that I can see:
- split out all scripts into two; or
- set AJAX_SCRIPT to true in lib/setup.php if a truthful AJAX parameter was sent.
The second option feels much dirtier - what if someone calls their field json instead of ajax for example, or what if ajax was the name of a variable on the page for some other reason?
If we go for the option of splitting them out, then I think that we should standardise on a naming scheme for such things to indicate the purpose.
Mahara have standardised on a scheme I personally quite like:
Standard script: foo.php
AJAX script: foo.ajax.php