Forum: Ruby on Rails prototype ajax + xml response

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kevin K. (Guest)
on 2006-02-23 06:41
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.
Daniel H. (Guest)
on 2006-02-23 07:15
(Received via mailing list)
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/0...

Hope this helps!
Daniel
Gregory S. (Guest)
on 2006-02-23 14:48
(Received via mailing list)
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/0...

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

} Hope this helps!
} Daniel
--Greg
Kevin K. (Guest)
on 2006-02-23 15:19
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/0...
>
> Hope this helps!
> Daniel


Thanks a lot Daniel. I appreciate it. I will try this.
Kevin
Tony Stevens (Guest)
on 2006-02-24 03:02
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.
Gregory S. (Guest)
on 2006-02-24 04:31
(Received via mailing list)
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
Tony Stevens (Guest)
on 2006-02-24 08:23
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.
This topic is locked and can not be replied to.