XMLHttpRequest

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
We have some code that works fine in IE6 but does not seem to work in IE7.

When I do a

request.responseText

I can see the contents of my XML. However when I try to retrieve the contents using

request.responseXML

I get no data. I then checked to see if I was getting any errors and found the following

request.responseXML.parseError.errorCode = -1072896681
request.responseXML.parseError.reason = Invalid xml declaration

Further searching on the error in Google did not get me very far.

The XML that I'm testing with is pretty simple and is a subset of the full xml. I can't figure out what is wrong with it as I can open the XML in a browser if I click on it directly.

<?xml version="1.0" encoding="ISO-8859-1"?>
<VEHICLES><FOUND>50</FOUND><LIMIT>50</LIMIT></VEHICLES>

I?m hoping someone can help or direct me to another forum where I could find some help.
 

GoatMonkey

Golden Member
Feb 25, 2005
1,253
0
0
If those suggestions don't work, try using this function to create XML from the responseText.


EDIT: Attach Code button doesn't seem to be working very well

function createXMLFromString (xmlStringIn) {
var xmlParser;
var msxmlParser;
var xmlDocOut;
try {
if (window.DOMParser && !XMLDocument.loadXML){
//mozilla
xmlParser = new DOMParser();
xmlDocOut = xmlParser.parseFromString(xmlStringIn, 'text/xml');
return xmlDocOut;
} else {
//ie
msxmlParser = new ActiveXObject("Msxml2.DOMDocument.4.0");
msxmlParser.loadXML(xmlStringIn);
xmlDocOut = msxmlParser;
return xmlDocOut;
}
}
catch (e) {
output("Can't create XML document.");
return null;
}
}
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
jjones I tried it without the encoding, same error.

Beau, I verified that we are setting the correct content-type on the server.

Well I've been able to narrow things down. IE7 has implemented the native version of XMLHttpRequest. If I turn this functionality off in the IE7 browser then it defaults to the ActiveXObject (What it used pre IE7) and everything works as it should. At least I have a work around.

I have not been able to find any widely reported problems with the MS implementation of XMLHttpRequest. I've also verified that our code meets the XMLHttpRequest standards.

I was hoping to avoid parsing the responseText but it looks like I may have to go down that road. Thanks GoatMonkey for the code.

 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
GoatMonkey I tried implementing your code. I pass the responseText to the function. It goes into the ie section. What does it return and what do I need to do with it?

When I do

alert(xmlDocOut)

It simply says [object].

How do I get it in a form so I can do

request.responseXML.getElementsByTagName( <my tag> )

Thanks


 

fs5

Lifer
Jun 10, 2000
11,774
1
0
just wondering, but do you require xml data returned? why not use JSON and not deal w/ all the xml parsing garbage...
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
fs5 this is an existing system that I'm trying to fix not a new system. As it is I've had to learn how AJAX works and the finer detail of XML having never dealt with either of them.
 

GoatMonkey

Golden Member
Feb 25, 2005
1,253
0
0
What is returned should be pretty much the same as what you would normally get from request.responseXML. So, put it in the callback function of your AJAX code where and set the return object = to a variable that you can then use in place of where you had request.responseXML before.
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
GoatMonkey and anyone else that happens to see this. I read that MS will discontinue support for Msxml2.DOMDocument.4.0. They recommend that you go with Msxml2.DOMDocument.3.0 or the latest version Msxml2.DOMDocument.6.0.
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
Originally posted by: GoatMonkey
What is returned should be pretty much the same as what you would normally get from request.responseXML. So, put it in the callback function of your AJAX code where and set the return object = to a variable that you can then use in place of where you had request.responseXML before.

Ok is this right? The first line is my original code where IE7 seems to return a length of 0. If it does then I call your code. When I check the length of test 2 after processing I still get 0.

var variablesObj = requester.responseXML.getElementsByTagName( <my tag> );
alert("variablesObj.length "+ variablesObj.length)
if (variablesObj.length == 0)
{
var test = createXMLFromString(requester.responseText);
var test2 = test.getElementsByTagName( <my tag> );
alert("test2.length "+ test2.length );
}

I'm hoping that I will not have to parse "request.responseText" manually by looking at the tags but I'm at a loss to understand why this is happening.
 

GoatMonkey

Golden Member
Feb 25, 2005
1,253
0
0
Before you run the createXMLFromString function what is the value of requester.responseText?

Also, try whay cyclistca mentioned and see if msxml2.DomDocument.6.0 or 3.0 makes any differenct when running this code. And try the original function in Firefox also to see if it works there, it's actually been a while since I've used that code.
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
ha ha I am cyclist ca ;) I tried that already.

The value of requester.responseText is the contents of the xml that I am creating. I've been reading mixed things about MS native implementation of XMLHttpRequest. Some people say that responseXML works and some don't. In the end I solved my problem by trying to create an ActiveX object before trying to create a native XMLHttpRequest object. Seems to do the trick.