• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
My First Ajax Request
 A HunBug Document
Last Updated:
Sep 07
Author:
HunBugView the document in fullAjax is a term used for dynamically changing the content of a web page without theneed to refreshing the page. It is not a language in itself but a framework that usesJavascript's XMLHttpRequest method to pass and retrieve data from the server, alongwith HTML and CSS elements to display the result. It is an acronym of "AsynchronousJavaScript and XML", although it does not always have to involve XML, text of any formatcan be returned. It is not a language in itself but a method that uses Javascript
A Simple Ajax Request
The example below will display a button, which when pressed displays the html sourceof the page via a XMLHttpRequest call.
<html><head><script type="text/javascript" language="javascript">function makeHttpRequest(){//===============================// Define http_request var httpRequest;try{httpRequest = new XMLHttpRequest(); // Mozilla, Safari, etc}catch(trymicrosoft){try{httpRequest = new ActiveXObject("Msxml2.XMLHTTP");}catch(oldermicrosoft){try{httpRequest = new ActiveXObject("Microsoft.XMLHTTP");}catch(failed){httpRequest = false;}}}if(!httpRequest)
 
{alert('Your browser does not support Ajax.');return false;}//===============================// Action http_requesthttpRequest.onreadystatechange = function(){if(httpRequest.readyState == 4)if(httpRequest.status == 200)alert(httpRequest.responseText);elsealert('Request Error: '+httpRequest.status);}httpRequest.open('GET',document.location.href,true);httpRequest.send(null); }</script></head><body><input type="button" value="My First Ajax Request"onclick="makeHttpRequest()"/></body> </html> 
How It Works
The makeHttpRequest function is called when the button is pressed. The first part of thefunction sets up the httpRequest variable. As usual Internet Explorer likes to do thingsdifferently, so we have to try and catch a few times to get it right. The section of codebelow shows the try and catch method which is the start of most Ajax functions.
//===============================// Define http_request var httpRequest;try{httpRequest = new XMLHttpRequest(); // Mozilla, Safari, etc}catch(trymicrosoft){try{httpRequest = new ActiveXObject("Msxml2.XMLHTTP");}catch(oldermicrosoft){try{httpRequest = new ActiveXObject("Microsoft.XMLHTTP");}catch(failed){
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...