Reuben Avery
2007-Sep-27 19:05 UTC
How to check that AjaxRequest.transport.responseXML is really XML?
Hi all, I am trying to figure out how to check that an AjaxRequest.transport.responseXML object is actually a valid Document before I go on trying to parse it. This is my thinking which works in Firefox but of course bombs IE with no Javascript error: if ( transport.responseXML instanceof Document ) { //parse the xml } else { // error handling } This is really driving me nuts! IE hits that if() expression and just "stops" the rest of the Javascript processing. No errors or anything. I''m trying to get some error handling for the situation when the XML being read in is not capable of being turned into a DOM Document tree either because the content-type is wrong or any other situation which would result in the Document not being available. Thanks for any help :-) Reuben --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Reuben Avery
2007-Sep-27 19:12 UTC
Re: How to check that AjaxRequest.transport.responseXML is really XML?
I thought it might be good to post the whole chunk of code.. maybe might be more helpful to ya''ll locsAjaxRequest = new Ajax.Request( xmlUrl, { method: ''get'', requestHeaders: { ''Accept'' : ''text/xml'', ''Cache-Control'': ''no-store'' }, onSuccess: function() { var transport = locsAjaxRequest.transport; debug(''Ajax.Request Success ('' + transport.status + ''/'' + transport.statusText + '' '' + locsAjaxRequest.getHeader(''content- type'') + '')'' ); if ( transport.responseXML instanceof Document ) { alert(''got here''); locsXml = transport.responseXML; if ( getLocationsFromXml( transport.responseXML ) ) { setupMap(); addLocationsToMap(); } else { dieGracefully(''Locations data unavailable''); } } else { dieGracefully( ''Unable to parse XML, is the content-type correct? (received '' + locsAjaxRequest.getHeader(''content-type'') + '')'' ); } }, onFailure: function() { dieGracefully(''Ajax.Request Failure! ('' + transport.status + ''/'' + transport.statusText + '')'' ); } }); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Matt Foster
2007-Sep-27 19:48 UTC
Re: How to check that AjaxRequest.transport.responseXML is really XML?
Hmm, i wonder if it''d be instance of XMLDocument, i am not sure but you may run into pantloads of browser inconsistencies with this predicate. You could use a common property of an XML document, and if it is a valid DOM object should contain it. if(transport.responseXML.firstChild) alternatively you could look into validating your XML against a schema. Cheers, Matt On Sep 27, 3:12 pm, Reuben Avery <reubid...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I thought it might be good to post the whole chunk of code.. maybe > might be more helpful to ya''ll > > locsAjaxRequest = new Ajax.Request( xmlUrl, { > method: ''get'', > requestHeaders: { ''Accept'' : ''text/xml'', ''Cache-Control'': > ''no-store'' }, > onSuccess: function() { > var transport = locsAjaxRequest.transport; > > debug(''Ajax.Request Success ('' + transport.status + ''/'' + > transport.statusText + '' '' + locsAjaxRequest.getHeader(''content- > type'') + '')'' ); > > if ( transport.responseXML instanceof Document ) > { alert(''got here''); > locsXml = transport.responseXML; > > if ( getLocationsFromXml( transport.responseXML ) ) { > setupMap(); > addLocationsToMap(); > } else { > dieGracefully(''Locations data unavailable''); > } > } else { > dieGracefully( ''Unable to parse XML, is the content-type > correct? (received '' + locsAjaxRequest.getHeader(''content-type'') + > '')'' ); > } > }, > onFailure: function() { > dieGracefully(''Ajax.Request Failure! ('' + transport.status > + ''/'' + transport.statusText + '')'' ); > } > });--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Reuben Avery
2007-Sep-27 19:57 UTC
Re: How to check that AjaxRequest.transport.responseXML is really XML?
Well, if ( transport.responseXML.firstChild ) does seem to work in IE.. however, now Firefox bombs with no error when it hits that line!!! # $@#!!!! I was also trying if ( XMLDocument.prototype.isPrototypeOf( transport.responseXML ) ) -- worked in Firefox, bombs in IE if ( transport.responseXML instanceof XMLDocument ) -- ditto, worked in firefox, bombs in IE argh!!!!!! can''t i even get a stupid error message from either of these? driving me crazy... thanks for your help Matt On Sep 27, 12:48 pm, Matt Foster <mattfoste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmm, i wonder if it''d be instance of XMLDocument, i am not sure but > you may run into pantloads of browser inconsistencies with this > predicate. > > You could use a common property of an XML document, and if it is a > valid DOM object should contain it. > > if(transport.responseXML.firstChild) > > alternatively you could look into validating your XML against a > schema. > > Cheers, > Matt > > On Sep 27, 3:12 pm, Reuben Avery <reubid...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I thought it might be good to post the whole chunk of code.. maybe > > might be more helpful to ya''ll > > > locsAjaxRequest = new Ajax.Request( xmlUrl, { > > method: ''get'', > > requestHeaders: { ''Accept'' : ''text/xml'', ''Cache-Control'': > > ''no-store'' }, > > onSuccess: function() { > > var transport = locsAjaxRequest.transport; > > > debug(''Ajax.Request Success ('' + transport.status + ''/'' + > > transport.statusText + '' '' + locsAjaxRequest.getHeader(''content- > > type'') + '')'' ); > > > if ( transport.responseXML instanceof Document ) > > { alert(''got here''); > > locsXml = transport.responseXML; > > > if ( getLocationsFromXml( transport.responseXML ) ) { > > setupMap(); > > addLocationsToMap(); > > } else { > > dieGracefully(''Locations data unavailable''); > > } > > } else { > > dieGracefully( ''Unable to parse XML, is the content-type > > correct? (received '' + locsAjaxRequest.getHeader(''content-type'') + > > '')'' ); > > } > > }, > > onFailure: function() { > > dieGracefully(''Ajax.Request Failure! ('' + transport.status > > + ''/'' + transport.statusText + '')'' ); > > } > > });--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Reuben Avery
2007-Sep-27 20:11 UTC
Re: How to check that AjaxRequest.transport.responseXML is really XML?
yay, i cracked it! this seems to work both in IE 6, Firefox, and Safari: if ( ((''responseXML'' in transport) && transport.responseXML != null) && transport.responseXML.firstChild ) sure is ugly to me, but i guess it works so thats that.. what do you think? and why can''t I get either "instanceof" or "isPrototypeOf()" to work for this? isn''t this the whole point of those two? bah --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---