...

View Full Version : Beginners AJAX Problem



Spacetech
04-16-2006, 05:55 PM
Hi Im new to ajax and I am having a problem with this script

ajax.js:

function createRequestObject() {
var connect;
var browser = navigator.appName;
if(browser == 'Microsoft Internet Explorer'){
connect = new ActiveXObject('Microsoft.XMLHTTP');
}
else{
connect = new XMLHttpRequest();
}
return connect;
}

var http = createRequestObject();

function ajax(url, id) {
load.style.display = 'block';
http.open("GET", url, true);
http.onreadystatechange = handleResponse(id);
http.send(null);
}

function handleResponse(id) {
if(http.readyState == 4 && http.status == 200){
document.getElementById(id).innerHTML = http.responseText;
load.style.display = 'none';
}
else{
alert("AJAX Error. Please Reload The Page");
}
}


test.html:

<script language="JavaScript" src="ajax.js" type="text/javascript"></script>
<style type="text/css">
#load {
position:absolute;
left:0;
top:0;
width:70px;
height:20px;
background-color: #CC4444;
color: white;
display: none;
}
</style>

<div id="load">Loading...</div>

<a href="javascript:ajax('test.txt', 'content')">Load Something</a>

<br />
<br />

<div id="content">
</div>

I get that alert message at the bottom when I try to run it.

Any ideas on how to fix it?

eak
04-16-2006, 06:05 PM
when you set http.onreadystatechange it should not be a function call.
for example
http.onreadystatechange = handleResponse(id);
should be
http.onreadystatechange = handleResponse;
or
http.onreadystatechange = function(){handleResponse(id);}

Spacetech
04-16-2006, 06:08 PM
It worked. Thanks!!!!

felgall
04-16-2006, 11:46 PM
Your code is calling the wrong processing for most versions of Internet Explorer (and browsers masquerading as Internet Explorer).

IE7 (and all browsers except IE) should be using XMLHttpRequest();
IE6 can most likely use "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", or at worst "MSXML2.XmlHttp.3.0".
Earlier versions of IE may also be able to use one of these (depending on what patches have been installed) and only if none of these are available should "MSXML2.XmlHttp" or Microsoft.XmlHttp" be used.

See http://javascript.about.com/library/blajax01.htm for a way to code the request so that it gets the best possible connection in each case.

boxxer03
04-18-2006, 11:47 PM
This may be a little off topic, but I just wanted to let EAK know that his signature about doing good deeds is frickin' awesome. :thumbsup:

SpirtOfGrandeur
04-19-2006, 11:53 AM
Your code is calling the wrong processing for most versions of Internet Explorer (and browsers masquerading as Internet Explorer).

IE7 (and all browsers except IE) should be using XMLHttpRequest();
IE6 can most likely use "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", or at worst "MSXML2.XmlHttp.3.0".
Earlier versions of IE may also be able to use one of these (depending on what patches have been installed) and only if none of these are available should "MSXML2.XmlHttp" or Microsoft.XmlHttp" be used.

See http://javascript.about.com/library/blajax01.htm for a way to code the request so that it gets the best possible connection in each case.

Thank you for that sir... thank you very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum