AJAX not working with Firefox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: AJAX not working with Firefox

Hybrid View

  1. #1
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    AJAX not working with Firefox

    This AJAX works fine in IE 6, but nothing happens in Firefox


    This is the html of the page:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Resolution Center Testing</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <script type="text/javascript" src="../js/sorttable.js"></script>
    <script type="text/javascript" src="js/rpc.js"></script>
    </head>
    <body>
    <table><tr><td>
    <div id="menu">
    
    
    <input type="button" class="link" value="Create/Edit Users" onclick="getUsers();"/><br/><br/>
    <input type="button" class="link" value="Create/Edit Test" onclick="getTests();"/><br/><br/>
    <input type="button" class="link" value="Unlock Test" onclick="getUnlock();"/><br/><br/>
    <input type="button" class="link" value="Grade Test" onclick="getGrade();"/><br/><br/>
    <input type="button" class="link" value="View Active Test" onclick="getActive();"/><br/><br/>
    
    </div>
    
    </td><td>
    
    <div id="admin_box">
    </div>
    
    
    
    </td></tr></table>
    
    
    
    </body>
    </html>
    clicking on of the buttons calls the proper javascript function, which makes the http request, and dumps that into the "admin_box" div

    here is the javascript:

    Code:
    function createRequestObject() {
            var ro;
            var browser = navigator.appName;
            if(browser == "Microsoft Internet Explorer"){
                    ro = new ActiveXObject("Microsoft.XMLHTTP");
            }else{
            ro = new XMLHttpRequest();
            }
            return ro;
    }
    
    var http = createRequestObject();
    
    
    function getUsers() {
            http.open('get', 'rpc/users.php?rand=' + Math.random() );
            http.onreadystatechange = handleAdmin;
            http.send(null);
    }
    
    function getTests() {
            http.open('get', 'rpc/tests/main.php?rand=' + Math.random() );
            http.onreadystatechange = handleAdmin;
            http.send(null);
    }
    
    function getUnlock() {
            http.open('get', 'rpc/unlock.php?rand=' + Math.random());
            http.onreadystatechange = handleAdmin;
            http.send(null);
    }
    
    function getGrade() {
            http.open('get', 'rpc/grade.php?rand=' + Math.random());
            http.onreadystatechange = handleAdmin;
            http.send(null);
    }
    
    function getActive() {
            http.open('get', 'rpc/active.php?rand=' + Math.random());
            http.onreadystatechange = handleAdmin;
            http.send(null);
    }
    
    function handleAdmin() {
            if (http.readystate == 4) {
                    document.getElementById('admin_box').innerHTML = http.responseText;
            }
    }
    All of this works perfectly fine in IE6, but not in Firefox.
    In firefox the buttons show up, but if you click any of them, nothing happens at all. An no errors show in the error console.

    The thing that really bothers me about this is there is another part of my web site that works fine in firefox and it uses IDENTICAL javascript (with the exception of URLs of course).

    If anyone could give me any guidance on this issue, I would be extremely grateful.

    Thank you for your time
    Last edited by PeejAvery; October 22nd, 2008 at 05:47 PM. Reason: Added code tags

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: AJAX not working with Firefox

    It's because you aren't creating your AJAX object correctly. Use the following instead.

    Code:
    var ro;
    try {ro = new XMLHttpRequest();}
    catch(e) {
      try {ro = new ActiveXObject("Msxml2.XMLHTTP");}
      catch(e) {
        try {ro = new ActiveXObject("Microsoft.XMLHTTP");}
        catch(e) {
          alert("Your browser does not support AJAX!");
          return false;
        }
      }
    }
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: AJAX not working with Firefox

    Thank you for your help PeejAvery

    first I tried this:

    Code:
    function createRequestObject() {
    var ro;
    try {ro = new XMLHttpRequest();}
    catch(e) {
      try {ro = new ActiveXObject("Msxml2.XMLHTTP");}
        catch(e) {
            try {ro = new ActiveXObject("Microsoft.XMLHTTP");}
                catch(e) {
                      alert("Your browser does not support AJAX!");
                            return false;
                                }
                                  }
                                  }
    }
    
    var http = createRequestObject();

    It does not work in IE 6 and firefox gave me an error that 'http has no properties'

    this was because the function was not returning anything.
    so i modified it to this:

    Code:
    function createRequestObject() {
    var ro;
    try {ro = new XMLHttpRequest();return ro;}
    catch(e) {
      try {ro = new ActiveXObject("Msxml2.XMLHTTP");return ro;}
        catch(e) {
            try {ro = new ActiveXObject("Microsoft.XMLHTTP");return ro;}
                catch(e) {
                      alert("Your browser does not support AJAX!");
                            return false;
                                }
                                  }
                                  }
    }
    
    var http = createRequestObject();

    Now, IE 6 works, but firefox still does nothing at all and gives no errors.

    What am I doing wrong here?
    Last edited by Nightwolf629; October 23rd, 2008 at 07:20 AM. Reason: pasted wrong code

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: AJAX not working with Firefox

    Well, you aren't returning the ro object. If there is no return, you can't set a variable equal to a function.

    Code:
    function createRequestObject() {
      var ro;
      try {ro = new XMLHttpRequest();}
      catch(e) {
        try {ro = new ActiveXObject("Msxml2.XMLHTTP");}
        catch(e) {
          try {ro = new ActiveXObject("Microsoft.XMLHTTP");}
          catch(e) {
            alert("Your browser does not support AJAX!");
            return false;
          }
        }
      }
      return ro;
    }
    
    var http = createRequestObject();
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: AJAX not working with Firefox

    I was returning the ro object in the second example i posted before.


    i just copied in your code and it still does not work.

    Still works in IE6 though, nothing in firefox.


    ... im at a loss, i have no idea what to do at this point.
    As far as I can tell, there is no reason firefox would not be working.

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: AJAX not working with Firefox

    I see that now. Strange placement for a return.

    Anyway...concerning your problem. I would suggest putting a bunch of alert() calls in your code. Then, see exactly where Firefox is failing.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center