Debugging JSON-based CFC Methods
This guide is intended for debugging cfc functions that are called via Ajax or a jQuery post that pass parameters via JSON and, in particular, return JSON, which makes it hard to debug the functions. Debugging is complicated due to the lack of ability to direct debug output to the page (since the page is expecting JSON, the debug output simply throws a javascript JSON parsing error). Before we begin:
Make sure that the cfc method is remote-enabled (e.g. function definition starts with "remote" keyword).
Make sure that, if you're calling via Ajax and looking for the function to return JSON for Ajax, the return type is set to "any". (e.g. "remote any myFunction() {}").
If the function is intended to work with jQuery dataTables for server-side pagination and processing, then additional arguments need to be defined, such as "Draw", "Start", and "Length". Additionally, dataTables will pass other parameters, such as a column reference for sorting, keywords, etc. It is best to find an existing cfc function method written to work with dataTables and use that as the starting point.
Debugging these functions is essentially comprised of dumping the desired debug output into files using the fileWrite() function. For example, issuing the following command will create a debug file in the /fi/test/debug directory:
fileWrite("C:\websites\FI_Dev\FI\test\debug\debugFile1.html", "[stuff that you want to output]").
Once data has been dumped into a file on the server, you can simply point your browser to that file to see the content.
If you want to output the result of a complex object instead of a string, you can use the writeDebug() function as follows:
Debugging is typically performed by
Outputing (to a file) the parameters that are being passed as part of the function invoking. This is to ensure that you're passing the data that you will need for the query or whatever other action you're doing in the function. You can do this by using commands like:
2. Outputing the results of the query to yet another file can be helpful to see if the query is producing the desired results. Again, this can be done by directing the outout to another file (so you don't overwrite the first file that is showing you the function arguments being passed:
If the data being passed is valid and the query is producing the desired results, then the next step is to see if the data is being compiled as JSON if the function is supposed to be return JSON. JSON encoding is typically performed by calling the SerializeJSON() function and simply feeding it the query results. However, in the case of server-side processing of jQuery dataTables data, data must be returned to the dataTables library that includes additional information that indicates what page the table is on, how many pages there are, what the starting record number is, what the sorting column is, etc. As such, the JSON is manually formatted, which can result in syntax errors. This JSON is typically created using the saveContent function and is saved as a var, such as "returnJSON". As such, this data can be dumped into a fileWrite() just like the previous query and arguments data.
Typical JSON will look like the following (this is the JSON output for a dataTables feed:
Last updated
Was this helpful?