# Prototype ajax + xml response

**URL:** <https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212>\
**Category:** Rails\
**Created:** [February 23, 2006, 5:41am UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212 "2006-02-23T05:41:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 23, 2006, 5:41am UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/1 "2006-02-23T05:41:51Z")

</div>

Hi,  
I am looking for a solution to use ajax.request object.  
Return response text will be in xml format and I like to know how to  
parse that xml information, so that I can put those info into the “span”  
or “div” tags of my html page. Is there any easy way to parse that xml  
info using prototype library?

Thanks,  
kevin.

---

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 23, 2006, 6:15am UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/2 "2006-02-23T06:15:55Z")

</div>

Hi Kevin,

I lifted the following code:

onSaveComplete: function(response)  
{  
var ajaxResponse = Try.these(  
function() { return new  
DOMParser().parseFromString(response.responseText,  
‘text/xml’); },  
function() { var xmldom = new ActiveXObject(‘Microsoft.XMLDOM’);  
xmldom.loadXML(response.responseText); return xmldom; }  
);

var personid =  
ajaxResponse.getElementsByTagName(‘personid’)[0].firstChild.nodeValue;  
}

> From here:  
> [http://weblogs.macromedia.com/mesh/archives/2006/01/parsing\_xml\_in.cfm](http://weblogs.macromedia.com/mesh/archives/2006/01/parsing_xml_in.cfm)

Hope this helps!  
Daniel

---

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 23, 2006, 1:48pm UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/3 "2006-02-23T13:48:40Z")

</div>

On Wed, Feb 22, 2006 at 07:13:16PM -1000, Daniel H. wrote:  
} Hi Kevin,  
}  
} I lifted the following code:  
}  
} onSaveComplete: function(response)  
} {  
} var ajaxResponse = Try.these(  
} function() { return new  
DOMParser().parseFromString(response.responseText,  
} ‘text/xml’); },  
} function() { var xmldom = new ActiveXObject(‘Microsoft.XMLDOM’);  
} xmldom.loadXML(response.responseText); return xmldom; }  
} );  
}  
} var personid =  
} ajaxResponse.getElementsByTagName(‘personid’)[0].firstChild.nodeValue;  
} }  
}  
} \>From here:  
} [http://weblogs.macromedia.com/mesh/archives/2006/01/parsing\_xml\_in.cfm](http://weblogs.macromedia.com/mesh/archives/2006/01/parsing_xml_in.cfm)

Er, what’s wrong with response.responseXML? A parsed XML tree is already  
available from the XMLHttpRequest object.

} Hope this helps!  
} Daniel  
–Greg

---

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 23, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/4 "2006-02-23T14:19:27Z")

</div>

Daniel H. wrote:

> Hi Kevin,
> 
> I lifted the following code:
> 
> onSaveComplete: function(response)  
> {  
> var ajaxResponse = Try.these(  
> function() { return new  
> DOMParser().parseFromString(response.responseText,  
> ‘text/xml’); },  
> function() { var xmldom = new ActiveXObject(‘Microsoft.XMLDOM’);  
> xmldom.loadXML(response.responseText); return xmldom; }  
> );
> 
> var personid =  
> ajaxResponse.getElementsByTagName(‘personid’)[0].firstChild.nodeValue;  
> }
> 
> > From here:  
> > [http://weblogs.macromedia.com/mesh/archives/2006/01/parsing\_xml\_in.cfm](http://weblogs.macromedia.com/mesh/archives/2006/01/parsing_xml_in.cfm)
> 
> Hope this helps!  
> Daniel

Thanks a lot Daniel. I appreciate it. I will try this.  
Kevin

---

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 24, 2006, 2:02am UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/5 "2006-02-24T02:02:51Z")

</div>

Gregory S. wrote:

> Er, what’s wrong with response.responseXML? A parsed XML tree is already  
> available from the XMLHttpRequest object.
> 
> } Hope this helps!  
> } Daniel  
> –Greg

When I tried this (on FireFox IIRC) the responseXML property was null.  
I read somewhere that this can happen if the mime type isn’t set to  
“text/xml”, and I’ve concluded that prototype doesn’t have a way of  
setting this.

I’d love to be able to rely on .responseXML - do you know how to ensure  
it will be set?

Tony S.

---

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 24, 2006, 3:31am UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/6 "2006-02-24T03:31:30Z")

</div>

On Fri, Feb 24, 2006 at 02:02:51AM +0100, Tony Stevens wrote:  
} Gregory S. wrote:  
} \>  
} \> Er, what’s wrong with response.responseXML? A parsed XML tree is  
} \> already available from the XMLHttpRequest object.  
} \>  
} \> } Hope this helps!  
} \> } Daniel  
} \> --Greg  
}  
} When I tried this (on FireFox IIRC) the responseXML property was null.  
} I read somewhere that this can happen if the mime type isn’t set to  
} “text/xml”, and I’ve concluded that prototype doesn’t have a way of  
} setting this.  
}  
} I’d love to be able to rely on .responseXML - do you know how to  
ensure  
} it will be set?

Well, the MIME type needs to be set properly. I haven’t touched the  
prototype library, but my understanding is that it is solely on the  
JavaScript side, not the Ruby side. In your Rails action you need to set  
the MIME type.

If I am wrong and prototype is on the Rails side as well., modify it.  
You  
have the source, after all. Make sure it is setting the MIME type.

} Tony S.  
–Greg

---

<div class="post-metadata">

**Author:** ![kevinkusf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@kevinkusf](https://www.ruby-forum.com/u/kevinkusf)\
**Post date:** [February 24, 2006, 7:23am UTC](https://www.ruby-forum.com/t/prototype-ajax-xml-response/72212/7 "2006-02-24T07:23:27Z")

</div>

Gregory S. wrote:

> Well, the MIME type needs to be set properly. I haven’t touched the  
> prototype library, but my understanding is that it is solely on the  
> JavaScript side, not the Ruby side. In your Rails action you need to set  
> the MIME type.
> 
> If I am wrong and prototype is on the Rails side as well., modify it.  
> You  
> have the source, after all. Make sure it is setting the MIME type.
> 
> } Tony S.  
> –Greg

Thankyou! That was just the help I needed.

Once I realised the problem was that the rails action wasn’t saying that  
it was sending XML it gave me a place to start looking. After a while I  
figured out how to set the response header.

It’s all about how you look at it …

Tony S.
