Jump to content

Trying to get Ajax to work


watagal
 Share

Recommended Posts

Greetings,I'm new at AJAX, reading "AJAX for Dummies" and can't get the first listing to work. Here's my HTML code (ajax.htm):

<html><head>	<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">	<title>AJAX Testing</title>	<script language="javascript" src="../Code/JS/ajax.js"></script>	<script language="javascript" src="../Code/JS/browser.js"></script>	<script language="javascript">		var oAjax = new newAjaxConnection();		if (!oAjax ) alert("Ajax Error initializing XMLHttpRequest!");	</script></head><body>	<h1>Fetching Data from Server:</h1>	<form>		<input type="button" value="Display Ajax Data" onclick="getAjaxData(oAjax,'file1.txt','divAjax');"><br>		<input type="button" value="Display Browser Type" onclick="checkBrowser('divBrowser');"><br>	</form>		<div id="divAjax"><p>The fetched data will go here</p></div>	<div id="divBrowser"></div></body></html>

The 'checkBrowser()' code works so I know my JS code is being loaded and I can write to a <div> section on my browser page.My 'ajax.js' code looks like this:

function newAjaxConnection(){	var o = false;	// if Microsoft	try {   o = new ActiveXObject("Msxml2.XMLHTTP");	}	catch (e) {  try { 	 o = new ActiveXObject("Microsoft.XMLHTTP");  }  catch (e2) { 	 o = false;  }	}		// if not Microsoft (Mozilla, Firefox, Safari, Opera, ...)	if (!o && typeof XMLHttpRequest != 'undefined')	o = new XMLHttpRequest();	return o;}	// newAjaxConnection()function getAjaxData(oConn,dataSource,divId) {	if (oConn) {  oConn.open("GET",dataSource,true);    oConn.onreadystatechange = function() { 	 if (oConn.readyState == 4 && oConn.ststus == 200) {    document.getElementById(divId).innerHTML = oConn.responseText; 	 }  }  oConn.send(null);	}}	// getAjaxData()

The 'file1.txt' resides in the same folder as 'ajax.htm' and contains one line that looks like this:

<h2>************** This was fetched from the server using Ajax *********************</h2>

I tried running it in IE6 and Firefox v1.0.7 - no luck. I'm hitting the wall and in dire need for some fresh eyes.Thanks,

Link to comment
Share on other sites

From what I understood I thought you had to either do this in XML with a return type sepcified, or you had to do it in php with a return type specified and an echo or return statement.One of the best things to use when debugging your JS is to try out the Firefox javascript consol so you can watch and tick off all the errors.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...