CF Coding Practices
  • General Coding Practices
  • Debugging JSON-based CFC Methods
    • Valid JSON Formatting
    • Validating JSON
    • Final Hints
  • How We Invoke Modals
  • Submit Handlers
  • Useful SQL Snippets
  • jQuery/JS How-Tos And Hints
  • jQuery dataTables Tricks
  • CFSCRIPT Snippets and Hints
  • StatusPage Integration
  • Windows Server Setup
  • Common CF Formatting Commands
  • CF Snippets & Hints
  • Developing With The Mura Platform
    • ContentRenderer.CFC
    • Iterators
    • Iterating Remote Feeds
    • Components
    • Nested Content and Interactive Page Assembly
    • Modules and Display Objects
Powered by GitBook
On this page

Was this helpful?

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:

  1. Make sure that the cfc method is remote-enabled (e.g. function definition starts with "remote" keyword).

  2. 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() {}").

  3. 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 must make sure that the desired directory exists before calling fileWrite() to create the new file.

If you want to output the result of a complex object instead of a string, you can use the writeDebug() function as follows:

savecontent variable="complexObject" {writeDump(queryObject)}; 
fileWrite('C:\websites\FI_Dev\FI\test\debug\debugFile1.html', 
    'My complex object data is:<br>#complexObject#');

Debugging is typically performed by

  1. 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:

savecontent variable="argData" {writeDump(arguments)}; 
fileWrite('C:\websites\FI_Dev\FI\test\debug\debugFile1.html', 
    'Arguments being passed are:<br>#complexObject#');

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:

savecontent variable="complexObject" {
    writeDump(queryObj.execute().getResult())
};
fileWrite('C:\websites\FI_Dev\FI\test\debug\debugFile2.html', 
    'My query results is:<br>#complexObject#');

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:

{
    "draw": 7,"recordsTotal": 5,"recordsFiltered": 5,"data": 
    [
        ["Jeff Nunziato", "$540.00", "Tom Ruff Company", "The Tuttle Agency", "0", "$9,000.00", "Outstanding","true","false","","01/19/2016"],
        ["Jeremy Servian", "$600.00", "Tom Ruff Company", "Normyle/Erstling Health Search Group", "0", "$10,000.00", "Outstanding","true","false","","04/21/2016"],
        ["Shannon Suarez", "$630.00", "Tom Ruff Company", "David Bagga Company", "0", "$10,500.00", "Outstanding","true","false","","01/17/2018"],
        ["Stephanee Cruz", "$570.00", "Tom Ruff Company", "David Bagga Company", "0", "$9,500.00", "Outstanding","true","false","","09/19/2017"],
        ["Steve Schloss", "$449.58", "Tom Ruff Company", "DeFazio & Associates", "0", "$7,493.00", "Outstanding","true","false","","04/21/2017"]
    ]
}

If you have extra quotation marks around the {} bracket symbols, unescaped characters, missing [] or {} symbols, etc. then the JSON will be invalid and not parse correctly. See more on this in the Valid JSON Formatting section below.

PreviousGeneral Coding PracticesNextValid JSON Formatting

Last updated 6 years ago

Was this helpful?