Debugging AJAX
- Use the transport mechanism to send debug information. For example, if you’re sending back an array of data, add a new element (or several), and put in the SQL query so you can see exactly what’s being executed.
- Store the returned data in global variables on the client side and use Firebug to examine them. console.debug and alert are helpful, too.
- Use var_dump and echo if you get frustrated. They’ll probably throw an error on the client side, but you’ll get to see what you want to.
- Use var_export($variable,true) if you want to do a var_dump to a string or to a file. file_put_contents(’/tmp/file’,$data,FILE_APPEND); is a great way to monitor script execution. Open a new SSH window and use tail -f /tmp/file to see it.
- Cut and paste output into Notepad so you can easily verify it. Often you need several chunks of text and data to truly understand what is happening and why.
- Develop complex pieces in a standalone environment. This is good for assembling arrays of data out of complex SQL queries. Simplifying the task so you can focus on the difficult parts will speed development.
- Watch out for extra commas in object assignments. IE will crash.
- If you have Visual Studio, use it. It can save you a tremendous amount of time when debugging under IE.
- Test for the presence of variables, objects, and attributes before using them. It isn’t really right, but you can use if (variable_exists) to see if the variable has been set up. A better solution is to initialize everything, or to use if (typeof variable != ‘undefined’).
- To remove something from a page, use style="display:none". It works, it’s simple, and it’s very low risk. If anyone is playing with your code, they’ll find it, but it shouldn’t be an issue unless it had security issues, or restricted content.
- Add a debug setting to your code that allows you to receive more information when errors occur. You can disable it for the production release.
Print article | This entry was posted by elvis on 02/11/10 at 10:01:00 pm . Follow any responses to this post through RSS 2.0. |