Fix error handling in ajax() function