Java Reference
In-Depth Information
complicated that redirecting the hidden frame to a different web page. This redirection auto-
matically triggers the second step.
2.
The request is sent to the server, which processes the data.
3.
The server sends its response (a web page) back to the hidden frame.
4.
The browser loads the web page in the hidden frame and executes any JavaScript code to
contact the visible frame.
The example in this section is based on the form validator built earlier in the chapter, but you'll use
a hidden iframe to facilitate the communication between the browser and the server instead of an
XMLHttpRequest object. Before getting into the code, you should first know about the data received
from the server.
the Server response
You expected a JSON data structure as the server's response when using XMLHttpRequest to get
data from the server. The response in this example is different and must consist of two things:
The data, which must be in HTML format
A mechanism to contact the parent document when the iframe receives the HTML response
The following code is an example of the response HTML page:
<!DOCTYPE html>
 
<html lang="en">
<head>
<title>Returned Data</title>
</head>
<body>
<script>
//more code here
</script>
</body>
</html>
This simple HTML page contains a single <script/> element in the body of the document.
The JavaScript code contained in this script block is generated by the PHP application, calling
handleResponse() in the visible frame and passing it the expected JSON.
The JSON data structure has a new member: the value field. It contains the username or e‐mail that
was sent in the request. Therefore, the following HTML document is a valid response from the PHP
application:
<!DOCTYPE html>
 
<html lang="en">
<head>
<title>Returned Data</title>
</head>
<body>
 
Search WWH ::




Custom Search