Improved error handling

- better handling for PHP errors that caused JSON not to be parsed correctly
- better Jquery fail code