Handling errors in AJAX requests
Errors are inevitable. Period. Sometimes things are not in your control—like server failures—and in this case you must have an error handling mechanism in place, which can catch the errors and show them to the users. Throughout the recipes in this chapter we have implemented callback functions that execute when a request is successful. It may happen (and I promise you it will happen) that you typed a filename incorrectly or the server encounters an error and you get an error rather than a successful response.
This recipe will explain how to deal with such situations in AJAX requests.
Getting ready
Create a folder Recipe7
inside the chapter2
folder.
How to do it...
Create a file named
index.html
in theRecipe7
folder. Define some CSS styles in it and create an input box that will ask for a filename to load and a button. Also create a paragraph where contents loaded in a file will be displayed.<html> <head> <title>Error handling</title...