CS75 Lecture #7 Part-1 "AJAX" with jQuery - Building Dynamic Websites
Course Web Site by CS75.tv
Course Sillabus (Opens in a PDF)
Lecture Slides for Lecture 7 (Opens in a PDF)
Lecture 7 Source Codes
Lecture 7 Source Codes in PDF
Javascript to process HTTP XML DOM
Course Sillabus (Opens in a PDF)
Lecture Slides for Lecture 7 (Opens in a PDF)
Lecture 7 Source Codes
Lecture 7 Source Codes in PDF
Javascript to process HTTP XML DOM
Lecture 0: HTTP |
Lecture 1: PHP |
Lecture 2: PHP, Continued |
Lecture 3: MVC, XML |
Lecture 4: SQL |
Lecture 5: SQL, Continued |
Lecture 6: JavaScript |
Lecture 7: Ajax |
Lecture 8: Security |
Lecture 9: Scalability |
==============================
<!--
ajax1.html
Gets stock quote from quote1.php via Ajax, displaying result with alert().
David J. Malan
Computer Science S-75
Harvard Summer School
-->
<!DOCTYPE html>
<html>
<head>
<script>
// an XMLHttpRequest
var xhr = null;
/*
* void
* quote()
*
* Gets a quote.
*/
function quote()
{
// instantiate XMLHttpRequest object
try
{
xhr = new XMLHttpRequest();
}
catch (e)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// handle old browsers
if (xhr == null)
{
alert("Ajax not supported by your browser!");
return;
}
// construct URL
var url = "quote1.php?symbol=" + document.getElementById("symbol").value;
// get quote
xhr.onreadystatechange = handler;
xhr.open("GET", url, true);
xhr.send(null);
}
/*
* void
* handler()
*
* Handles the Ajax response.
*/
function handler()
{
// only handle loaded requests
if (xhr.readyState == 4)
{
if (xhr.status == 200)
alert(xhr.responseText);
else
alert("Error with Ajax call!");
}
}
</script>
<title></title>
</head>
<body>
<form action="quote1.php" method="get" onsubmit="quote(); return false;">
Symbol: <input id="symbol" name="symbol" type="text">
<br><br>
<input type="submit" value="Get Quote">
</form>
</body>
</html>
It's an object which comes with many methods as follows;
0,1,2,3, and 4. The call sequence from 0 to 2 goes fast. Most people pay attention to only the state 4 (loaded).
However, we are not interested in actually submitting a <form>.
Instead, we are making use of the HTTP background process that comes with submitting a <form>.
That's why the "RETURN" value is set to "false".
The variable "xhr" is initialized by setting it to be null.
No comments:
Post a Comment