Results 1 to 5 of 5

Thread: Ajax code not working in Firefox

  1. #1
    Join Date
    Nov 2008
    Posts
    774

    Ajax code not working in Firefox

    I have a small Ajax code that works very well in IE but do not work under Firefox. I have no error in Firefox console.

    Code:
    function get_Xhr()
    {
      if (window.XMLHttpRequest)  // Purpose of the current window 
    {
        xhr = new XMLHttpRequest();  // Firefox, Safari, ... 
    }
    else
       if (window.ActiveXObject) // Version Active 
       {
          xhr = new ActiveXObject("Microsoft.XMLHTTP" ); // Internet Explorer  
       }
    }
    function ajaxclient()
    {
      get_Xhr();
      xhr.onreadystatechange = function()
       {
        if(xhr.readyState == 4 && xhr.status == 200)
         {
          document.getElementById('testt').innerHTML = xhr.responseText;
         }
       }
      xhr.open("POST",'ajax/ajax_ajout_client.php',true);
      xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xhr.send("cocl="+document.form_ajout_client.name.value);
    }

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Ajax code not working in Firefox

    function ajaxclient()
    {
    get_Xhr();
    The get_Xhr() serves no function, since you can return any value. If you instantiate objects without sending them back (your variable is local) that will not work, it is clear and logical

    Instead use:

    Code:
    var xhr = get_Xhr ();

  3. #3
    Join Date
    May 2008
    Posts
    271

    Re: Ajax code not working in Firefox

    You can also add a test for Msxml2.XMLHTTP:

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

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Ajax code not working in Firefox

    After giving the second thought to your case, I think you should go with

    Code:
    var xhr = function ()
    {
        if (window.XMLHttpRequest) // FIREFOX
            return new XMLHttpRequest ();
     
        if (window.ActiveXObject) // IE old and new
        {
            var o;
            try { o = new ActiveXObject ("Msxml2.XMLHTTP" ); }
            catch (e) { o = new ActiveXObject ("Microsoft.XMLHTTP" ); }
            return o;
        }
        alert ("No support for XHR!" );
        return false;
    }
    it is probably more complete because it manages the different cases for IE

  5. #5
    Join Date
    Jan 2006
    Posts
    2

    Re: Ajax code not working in Firefox

    The following example should work mate

    <html>
    <body>
    <script type="text/javascript">
    function ajaxFunction()
    {
    var xmlHttp;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    try
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {
    alert("Your browser does not support AJAX!");
    return false;
    }
    }
    }
    }
    </script>
    <form name="myForm">
    Name: <input type="text" name="username" />
    Time: <input type="text" name="time" />
    </form>
    </body>
    </html>
    Last edited by nitinwalawalkar; 02-05-2009 at 02:41 PM. Reason: missed something

Similar Threads

  1. Firefox 3.6 cookies not working in fresh Firefox 6 beta
    By KalTak in forum Technology & Internet
    Replies: 5
    Last Post: 19-08-2011, 07:57 PM
  2. Ajax collapsible panel not working properly
    By jEEMOOT in forum Software Development
    Replies: 6
    Last Post: 26-02-2011, 09:55 AM
  3. jQuery AJAX vs. Adobe AIR AJAX Class
    By Agaev in forum Windows Software
    Replies: 5
    Last Post: 06-07-2010, 01:59 PM
  4. Firefox Error code : ssl_error_ssl_disabled
    By Ryan21 in forum Technology & Internet
    Replies: 4
    Last Post: 26-12-2009, 07:42 PM
  5. Need a working struts2 Ajax example
    By The_D in forum Software Development
    Replies: 3
    Last Post: 04-08-2009, 05:42 PM

Tags for this Thread

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
  •  
Page generated in 1,750,210,589.72941 seconds with 16 queries