Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with the Ajax responseText in Mozilla

RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 350
#1: Sep 16 '08
Hi All,
I am using Ajax inorder to retrieve a data from the db which is an xml and i am parsing the responseText into an xml. the code what i had tried is working well with IE, but the same makes a lot of trouble in mozilla any help would be greatful

the following is the code

[HTML] if (httpRequest.readyState == 4)
{
if(httpRequest.status == 200)
{
var pricingSummaryXML = httpRequest.responseText;
alert("Pricing Summary XML = "+pricingSummaryXML);
var xmlDoc = newXMLDoc(pricingSummaryXML);
alert("XML Doc = "+xmlDoc);
if(xmlDoc != null)
{
var root = xmlDoc.getElementsByTagName("response")[0]; //This line shows Error[/HTML]

[HTML]function newXMLDoc(xmlData)
{
var xmlDoc = null;
try { //Internet Explorer
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
alert("Inside 1");
xmlDoc.loadXML(xmlData);
}
catch(e)
{
try {
//Firefox, Mozilla, Opera, etc.
parser=new DOMParser();
alert("Inside 2")
xmlDoc=parser.parseFromString(xmlData,"text/xml");
}
catch(e) {
alert(e.message);
return null;
}
}
return xmlDoc;
} [/HTML]

In mozilla I am getting an exception with the description root is null, i tried it by displaying its length, it shows 0. I replaced the responseText with the XML directly what I am retrieveing from db it works but i want in the responseText only. Please i need some suggestions

Regards
Ramanan Kalirajan

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Sep 16 '08

re: problem with the Ajax responseText in Mozilla


Hi.

Have you tried using the responseXML property?

Like:
Expand|Select|Wrap|Line Numbers
  1. xmlDoc = request.responseXML;
  2.  
  3. // Get a list of <node> nodes
  4. nodes = xmlDoc.getElementsByTagName('node');
  5.  
  6. // Print the value of each <node>
  7. for(i = 0; i < nodes.length; i++) {
  8.     alert(nodes[i].childNodes[0].nodeValue);
  9. }
  10.  
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 350
#3: Sep 16 '08

re: problem with the Ajax responseText in Mozilla


Quote:

Originally Posted by Atli

Hi.

Have you tried using the responseXML property?

Like:

Expand|Select|Wrap|Line Numbers
  1. xmlDoc = request.responseXML;
  2.  
  3. // Get a list of <node> nodes
  4. nodes = xmlDoc.getElementsByTagName('node');
  5.  
  6. // Print the value of each <node>
  7. for(i = 0; i < nodes.length; i++) {
  8.     alert(nodes[i].childNodes[0].nodeValue);
  9. }
  10.  

I am sorry I am not having any idea on responseXML, wether it is available with normal Ajax or Prototype Ajax, since u had specfied with request.responseXML. pls tell me wether can i use it in normal ajax i.e. in httprequest

Regards
Ramanan Kalirajan
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#4: Sep 16 '08

re: problem with the Ajax responseText in Mozilla


you can use responseXML in normal AJAX (it's one of the standard properties: ajax methods and properties (scroll down a bit))

regards
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#5: Sep 16 '08

re: problem with the Ajax responseText in Mozilla


Yea. It's a part of the XMLHttpRequest object. (Note the XML part ;P)
Should be available on any browser that supports AJAX.

I've noticed, however, that if the requested file doesn't specify the Content-Type as "text/xml" (excluding actual .xml files), the responseXML property will be undefined.
Just something to keep in mind if your generating the XML using a server-side language.
Reply