Chapter 15.1.2. Making a Request

Let’s say that you’re not out to do anything too fancy. You just want to run a simple XMLHttpRequest to get some more data from the server. Here’s how you do that:

function showText(xmlhttp) {
    alert(xmlhttp.responseText);
}
d = doSimpleXMLHttpRequest("http://localhost:8080/yourcode");
d.addCallback(showText);

That’s some easy Ajax.

doSimpleXMLHttpRequest returns a Deferred. By making the call, we know that the XMLHttpRequest was made, but we have no idea when it will return. We just have the promise that we will either get a result or an error condition after the browser has finished with the asynchronous request.

By calling addCallback on the Deferred, we tell MochiKit where in our JavaScript code the ...

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.