Results 1 to 2 of 2

Thread: Need help with ajax in firefox 5+...

  1. #1
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with ajax in firefox 5+...

    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:

    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~
    Last edited by ShadowIce; 06-23-2009 at 03:11 PM.

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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. 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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •