I have developed a new website
My website functions smoothly on most browsers and iPhone simulators, but encounters issues when accessed from a real iPhone. I have traced the problem to XMLHttpRequest(). The XML data retrieval seems to be causing trouble, as it returns undefined when trying to read the children of [object Element]. This situation is quite perplexing and baffling for me.
Below is my code snippet for fetching the XML data:
function convertXml (path) {
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",path,false);
xmlhttp.send();
xml=xmlhttp.responseXML.getElementsByTagName("app");
var foo = [];
for (var i = 0; i <= xml.length-1; i++) {
foo[i] = {};
for (var j = 0; j <=xml[i].children.length-1; j++) {
foo[i][xml[i].children[j].tagName] = xml[i].children[j].innerHTML.trim();
}
}
return foo;
}
After extensive testing, I discovered that on an iPhone, the request returns an [object Document], while on a computer it returns an [object XMLDocument]. Although I am unsure of the significance of this difference, I believe it might be the root cause of my issue. Is there a way to convert between these types?
In an attempt to resolve the problem, I switched to using jQuery instead of plain JavaScript, but unfortunately, the issue still persists.
Here is the updated jQuery code:
function getXML (path) {
var response = $.ajax({ type: "GET",
url: "testingXml.xml",
async: false,
}).responseText;
parser=new DOMParser();
xmlDoc=parser.parseFromString(response,"text/xml");
return xmlDoc;
}
function xmlToObject (x) {
var xml = x.getElementsByTagName("app");
var foo = [];
for (var i = 0; i <= xml.length-1; i++) {
foo[i] = {};
for (var j = 0; j <=xml[i].children.length-1; j++) {
foo[i][xml[i].children[j].tagName] = xml[i].children[j].innerHTML.trim();
}
}
return foo;
}
To obtain the array, you can use this code
xmlToObject(getXML("testingXml.xml"))
The issue persists, functioning fine on a computer but not displaying properly on iPhone (Google, Safari, Firefox, Opera). It appears that the XML content is not being rendered appropriately on mobile devices.