PDA

View Full Version : Need help with ajax in firefox 5+...



ShadowIce
06-23-2009, 03:04 PM
Hi all. I've been studying ajax for a while now. The problem is it works great in IE, but not in firefox 5+ and opera..

I would like for this code to check if ajax exists and if it does, then read and return text to a div.. even if its in ff..

Here's the code:


<html>
<body>
<script type="text/javascript">
function readdata(news,nfile){

var txtFile = new XMLHttpRequest();
txtFile.open("GET", nfile, true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) { // Makes sure the document is ready to parse.
if (txtFile.status === 200) { // Makes sure it's found the file.
allText = txtFile.responseText;
document.getElementById(news).innerHTML = allText;
} else {
document.getElementById(news).innerHTML = 'News file not found';
}
}
}
txtFile.send();

}

function ajax(news,nfile){
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
alert(xmlHttp);
readdata(news,nfile);
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}
}
</script>
<div id="news"><script type="text/javascript">ajax('news','news.txt');</script></div>
<form name="myForm">
Name: <input type="text" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>

You will need to make news.txt in same location for this to work.

ANY help is GREATLY appreciated! :)

~SI~

Jesdisciple
06-23-2009, 06:24 PM
Firefox 5...? Do you mean 3.0.5?

Anyway, this is a rather unusual case; the standards-compliant browsers added a rule to the specification (http://www.w3.org/TR/XMLHttpRequest/). The spec says your code should work, but Firefox says (when I alert the first exception):
[Exception... "Not enough arguments [nsIXMLHttpRequest.send]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://localhost/lib/JS/Ajax/ :: readdata :: line 18" data: no]Changing txtFile.send(); to txtFile.send(null); fixes the problem.