Java Reference
In-Depth Information
Nothing has really changed for asynchronous requests. This is the exact same code used earlier in
the chapter. If you want to use synchronous communication, simply set async to false , like this:
function requestCallback(responseText) {
alert(responseText);
}
var http = new HttpRequest("test.txt", requestCallback);
http.async = false;
http.send();
You now have an Ajax module that requests information in both asynchronous and synchronous
communication!
exercise 2 Question
It was mentioned earlier in the chapter that you could modify the smart forms to not use hyperlinks.
Change the form that uses the HttpRequest module so that the Username and Email fields
are checked when the user submits the form. Listen for the form's submit event and cancel the
submission if a username or e‐mail is taken.
exercise 2 Solution
First, a disclaimer: Ideally, the service provided by ch14 _ formvalidator.php should allow you to
check both the username and e‐mail address with a single request. That would greatly simplify this
solution. However, sometimes you need to make multiple requests, and each request is sometimes
dependent upon the outcome of a previous request. This question and solution is meant to emulate
that.
Additionally, issuing multiple (and linked) asynchronous operations is a rather complex ordeal—a
condition referred to ”callback hell.” You'll get a taste of that in this solution.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 14: Question 2</title>
<style>
.fieldname {
text-align: right;
}
.submit {
text-align: right;
}
</style>
</head>
<body>
<form name="theForm">
Search WWH ::




Custom Search