this is so dumb [AJAX]

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
so, I normally work with synchronous server requests, because overuse of AJAX makes me want to be sick. However, I've had cause to do some AJAXing for the last couple of days, and it's so frustrating that it makes me wonder why it's so popular.

It saddens me to be a moaner about IE, it seems to be so cliche (damn Ubuntu's lack of accents!) but responseXML just DOESN'T work, and its DOM traversing/manipulation is just a joke. If what I've been trying to do wasn't so important from an interface point of view, I'd have ditched it ages ago!

luckily, though - I managed to find a [very convoluted] way to do it, which I thought i'd share to hopefully save one person some time!
from lengthy reading, responseXML is very temperamental at the best of times, but no matter how much I fiddled with my XML schema, it wouldn't work - so, I took to converting my responseText to an XML document (remember...convoluted) so I could actually play with my result-set in IE:

Code:
try { //internet explorer
  var response = new ActiveXObject("Microsoft.XMLDOM");
  response.loadXML(XmlHttp.responseText);
  users = response.documentElement.getElementsByTagName('user');
  if (users.length > 0) { //assigning variables
    var id = users[0].childNodes[0].text;
  }
} catch(e) { //user's browser does not suck
  response = XmlHttp.responseXML;
  users = response.documentElement.getElementsByTagName('user');
  if (users.length > 0) { //assigning variables
    var id = users[0].childNodes[0].textContent;
  }
}

obviously, I've made the assumption here that you've successfully connected to the server, and you're ready to receive a response!

once this is done, you may spot that the data between your tags is actually placed in a different property, depending on whether we're dealing with a responseXML object or an ActiveX object - AX stores it in text, whereas responseXML stores it in textContent - another useful feature!

Am I alone in thinking there should be some uniformity in the way this is done?! Does it really need to be this difficult?!

in case anyone can help me see why my XML isn't being returned, it follows, but it's very messy!!

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><message><user><id><![CDATA[af83deb7-8aa9-abb6-89f4-46b84b2ae56a
]]></id><user_name><![CDATA[Jasper]]></user_name><password><![CDATA[09b4bc4db5b4d2c676cec6c0805d70fe
]]></password><access><![CDATA[sdf]]></access><active><![CDATA[1]]></active><deleted><![CDATA[1]]></deleted
><date_modified><![CDATA[2007-08-1415:44:41]]></date_modified><date_entered><![CDATA[0000-00-00 00:00:00]]></date_entered><assigned_user_id><![CDATA[89a35711-c398-bb61-98b8-4565d3082212]]></assigned_user_id
><forename/><surname/></user><user><id><![CDATA[a8cdc970-39a6-b48e-2b23-46b84fdd4d31]]></id><user_name
><![CDATA[Jasper]]></user_name><password><![CDATA[f9db76131ff20a750059cf5a74ce72f7]]></password><access
><![CDATA[gjfdh]]></access><active><![CDATA[0]]></active><deleted><![CDATA[1]]></deleted><date_modified
><![CDATA[2007-08-07 11:00:48]]></date_modified><date_entered><![CDATA[2007-08-07 10:56:14]]></date_entered
><assigned_user_id><![CDATA[ea7f7eba-c2c6-f3f8-9710-45ec13f96867]]></assigned_user_id><forename/><surname
/></user><user><id><![CDATA[28bf5ea7-d4b0-d6d5-753b-46b97971e6eb]]></id><user_name><![CDATA[Jasper]]
></user_name><password><![CDATA[318034a30dede357d5f605d7d9bae770]]></password><access><![CDATA[aa]]>
</access><active><![CDATA[1]]></active><deleted><![CDATA[1]]></deleted><date_modified><![CDATA[2007-08-14
 15:35:23]]></date_modified><date_entered><![CDATA[2007-08-08 08:05:57]]></date_entered><assigned_user_id

><![CDATA[221a288a-6892-4a09-ae6a-46adfe35656e]]></assigned_user_id><forename/><surname/></user></message

>

don't worry, it's not live data!! :o
 
Back
Top Bottom