Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 01-05-2009
Member
 
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);
}
Reply With Quote
  #2  
Old 01-05-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Ajax code not working in Firefox

Quote:
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 ();
Reply With Quote
  #3  
Old 01-05-2009
XSI XSI is offline
Member
 
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; 
}
Reply With Quote
  #4  
Old 01-05-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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
Reply With Quote
  #5  
Old 02-05-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:33 PM.