Connecting Tech Pros Worldwide Help | Site Map

Ajax is not working in firefox and opera

Newbie
 
Join Date: May 2007
Posts: 28
#1: May 3 '07
I have the following javascripts which is working in IE, but not working in Firefox and opera.

Expand|Select|Wrap|Line Numbers
  1. var xmlhttp=null;
  2. function showCustomer(str)
  3. {
  4.   xmlhttp=getxmlhttp();
  5.  
  6. if (xmlhttp==null)
  7. {
  8.   alert("Your Browser does not support AJAX");
  9.   return;
  10.  }
  11.  
  12.  var url="search.php";
  13.  url=url+"?q="+str;
  14.   url=url+"&sid="+Math.random();
  15.    xmlhttp.onreadyStateChange=statechanged;
  16.    xmlhttp.open('get',url,false);
  17.       xmlhttp.send(null);
  18.  
  19.  
  20.   }  
  21.  
  22. function statechanged()
  23. {
  24.  alert("ok"); 
  25.  if (xmlhttp.readyState==4)
  26.  {
  27.  
  28.          document.getElementById("txt").innerHTML=xmlhttp.responseText;
  29.  
  30. }      
  31.  
  32.  }
  33.  
  34.  function getxmlhttp()
  35.  {
  36.    xmlhttp=null;
  37.    try
  38.   {
  39.   // Firefox, Opera 8.0+, Safari
  40.   xmlHttp=new XMLHttpRequest();
  41.   }
  42. catch (e)
  43.   {
  44.   // Internet Explorer
  45.   try
  46.     {
  47.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  48.     }
  49.   catch (e)
  50.     {
  51.     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  52.     }
  53.   }
  54. return xmlHttp;
  55. }

How to solve this problem.

Thanks in advance
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: May 3 '07

re: Ajax is not working in firefox and opera


Welcome to TSDN.

The probable reason why it is not working is that the global variable is "xmlhttp", but when using the XMLHttpRequest object, you have used "xmlHttp" (note the case difference).
Newbie
 
Join Date: May 2007
Posts: 28
#3: May 3 '07

re: Ajax is not working in firefox and opera


Quote:

Originally Posted by acoder

Welcome to TSDN.
The probable reason why it is not working is that the global variable is "xmlhttp", but when using the XMLHttpRequest object, you have used "xmlHttp" (note the case difference).


Thanks for the response.

I have changed all varables as "xmlhttp". But still above problem exists. ie It is working in IE and not in firefox and opera.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: May 3 '07

re: Ajax is not working in firefox and opera


Perhaps onreadyStateChange should be onreadystatechange.

What error do you get? Check the console for errors or Firebug if you have it.
Newbie
 
Join Date: May 2007
Posts: 28
#5: May 4 '07

re: Ajax is not working in firefox and opera


Quote:

Originally Posted by acoder

Perhaps onreadyStateChange should be onreadystatechange.

What error do you get? Check the console for errors or Firebug if you have it.


Thanks a lot

My code is working in firefox as well as in opera. I have changed onreadyStateChange to onreadystatechange .Then it is working in Opera, but not in firefox. . After this , I changed xmlhttp.open('GET',url,false) to xmlhttp.open('GET',url,true) .Now it is working IE, Opera and Firefox.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: May 4 '07

re: Ajax is not working in firefox and opera


Quote:

Originally Posted by benoypaul

Thanks a lot

My code is working in firefox as well as in opera. I have changed onreadyStateChange to onreadystatechange .Then it is working in Opera, but not in firefox. . After this , I changed xmlhttp.open('GET',url,false) to xmlhttp.open('GET',url,true) .Now it is working IE, Opera and Firefox.

It's good that you got it working, but how did it work in the other browsers with those mistakes. That last argument makes AJAX asynchronous.
Newbie
 
Join Date: Feb 2008
Posts: 1
#7: Feb 8 '08

re: Ajax is not working in firefox and opera


Quote:

Originally Posted by benoypaul

I have the following javascripts which is working in IE, but not working in Firefox and opera.

var xmlhttp=null;
function showCustomer(str)
{
xmlhttp=getxmlhttp();

if (xmlhttp==null)
{
alert("Your Browser does not support AJAX");
return;
}

var url="search.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadyStateChange=statechanged;
xmlhttp.open('get',url,false);
xmlhttp.send(null);


}

function statechanged()
{
alert("ok");
if (xmlhttp.readyState==4)
{

document.getElementById("txt").innerHTML=xmlhttp.r esponseText;

}

}

function getxmlhttp()
{
xmlhttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}


How to solve this problem.

Thanks in advance

*********************************

xmlhttp.open('get',url,false);

Replace with

xmlhttp.open('get',url,true);
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Feb 8 '08

re: Ajax is not working in firefox and opera


Hi, welcome to TSDN! Thanks for posting.
Quote:

Originally Posted by delhiloves10

*********************************

xmlhttp.open('get',url,false);

Replace with

xmlhttp.open('get',url,true);

benoypaul managed to solve this, but thanks anyway for pointing out the change required.
Newbie
 
Join Date: Sep 2008
Posts: 1
#9: Sep 30 '08

re: Ajax is not working in firefox and opera


Quote:

Originally Posted by benoypaul

...I changed xmlhttp.open('GET',url,false) to xmlhttp.open('GET',url,true) .Now it is working IE, Opera and Firefox.

Thank you for the info! Was having the same issue and this resolved it for me as well.
Reply