cakePHP and debug disruption
cakePHP and debug disruption
martin@sommer.net
Posts: 15Questions: 0Answers: 0
CakePHP has a debug function that appends additional HTML to my Ajax JSON response, which then breaks the ability for DataTables to read the JSON object.
For example, a standard response of:
{"sEcho": 1, "iTotalRecords": 3001, "iTotalDisplayRecords": 1, "aaData": [ ["data1","data2","data3","data4","data5","data6"]] }
... with debug turned on, becomes:
{"sEcho": 1, "iTotalRecords": 3001, "iTotalDisplayRecords": 1, "aaData": [ ["data1","data2","data3","data4","data5","data6"]] }<!-- cakephp debug data ........>
How can I make the DataTable "only" read the JSON object, and ignore all the rest?
Thanks,
Martin
For example, a standard response of:
{"sEcho": 1, "iTotalRecords": 3001, "iTotalDisplayRecords": 1, "aaData": [ ["data1","data2","data3","data4","data5","data6"]] }
... with debug turned on, becomes:
{"sEcho": 1, "iTotalRecords": 3001, "iTotalDisplayRecords": 1, "aaData": [ ["data1","data2","data3","data4","data5","data6"]] }<!-- cakephp debug data ........>
How can I make the DataTable "only" read the JSON object, and ignore all the rest?
Thanks,
Martin
This discussion has been closed.
Replies
I think you have two options here:
1. You can put the debug information into the JSON object something like this:
[code]
{
"sEcho": 1,
"iTotalRecords": 3001,
...
"sDebug": "debug information as a string"
}
[/code]
The advantage of doing it this way is that it is valid JSON, and therefore jQuery can parse it as such. Then you can use fnServerData ( http://datatables.net/examples/server_side/custom_vars.html ) to look at the JSON returned and output your debug however you want.
Just sticking the debug string on the end is not valid JSON, and thus you make the parsing step that little bit more complicated - however it can be done:
2. Using fnServerData again, and $.ajax() ( http://api.jquery.com/jQuery.ajax/ ) you can have jQuery treat the return as a string rather than a JSON object, so it won't be parsed automatically. This way you can then manipulate the string yourself and cut out the debug statement (putting in a known separator would be the easiest way of breaking up the string). Then eval the JSON and pass it on to DataTables.
If 1 is possible using the CakePHP output - I'd say go with that :-)
Regards,
Allan
I found a third way within cakePHP, which allows me to have debug set "on" for the application, while turning it "off" for returned ajax data.
First, within core.php, set:
Configure::write('debug', 2);
Second, within the ajax function:
$this->layout='ajax'; //Set the proper layout.
Configure::write('debug', 0); //Turn off debug info for ajax/json data.
This allows debug for the app as a whole, and clean json data passed for the ajax call.
Martin
Thanks for sharing a 'CakePHP' way to do it - hopefully others will find this of use as well!
Regards,
Allan