Chapter 15.1.3. Handling Errors

If your Ajax call has an incorrect URL or some kind of error occurs on the server, your client-side code on the browser needs to be able to handle those cases. MochiKit Deferred objects differentiate between successful calls and failures. The showText function in the preceding example is only going to be called if the Ajax call was a success. What if it fails?

In our example, it would fail silently. The user wouldn’t get a false indication of success, but he’s probably not going to be happy about being given the silent treatment.

We can add to the example to provide a useful error message:

function showText(xmlhttp) { alert(xmlhttp.responseText); } function showError(err) { alert("An error occurred! The status ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.