Tyler Tracy
Tyler Tracy

Reputation: 41

Website working, but not on iphone. XMLHttpRequest() error

I have created this website

http://www.tylertracy.com/testing/Xml/App%20veiwer%205-28.php

It works fine on most browsers and on iPhone simulators, but it doesn't work on a real iPhone. I have narrowed it down to the XMLHttpRequest(). It seems that when I am getting the xml, I can not read the children of [object Element] it returns undefined. This is very baffling i do not understand.

Here is my code for getting the XML

function convertXml (path) {
    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
    }else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
    }
    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++) { // get all children of the app and make it a part in the object
            foo[i][xml[i].children[j].tagName] = xml[i].children[j].innerHTML.trim();//super complicated
        } 
     }
    return foo;
}

After much experimenting i have discovered that on iPhone the request returns a [object Document] while the computer returns a [object XMLDocument]. I don't know what this means, but i feel like it is where my problem is coming from. is there a way of maybe converting between these?

I have since then updated the code to jQuery seeing if the issue still persists, which it does.

Here is the new jQuery

function getXML (path) {
//gets text version of the xml doc
var response = $.ajax({ type: "GET",   
                    url: "testingXml.xml",   
                    async: false,
                  }).responseText;
//converts text into xmlDoc
parser=new DOMParser();
xmlDoc=parser.parseFromString(response,"text/xml");
return xmlDoc;
}

function xmlToObject (x) {
var xml = x.getElementsByTagName("app"); //get all the xml from the root
var foo = [];
for (var i = 0; i <= xml.length-1; i++) {
    foo[i] = {}; // make the object
    for (var j = 0; j <=xml[i].children.length-1; j++) { //get all of the children of the main tag
        foo[i][xml[i].children[j].tagName] = xml[i].children[j].innerHTML.trim();  //super complicated
    }
}
return foo;
}

Then to get the array, you would write this code xmlToObject(getXML("testingXml.xml"))

The issue is still happening, on computer it is fine, but on iPhone (Google, Safari, Firefox, Oprah) It seems that the xml just isn't displaying.

Upvotes: 4

Views: 5881

Answers (1)

Guillaume Algis
Guillaume Algis

Reputation: 11006

I'm getting

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/

in the Google Chrome console when opening your page. This could be why it won't load on iOS: instead of just logging a warning, Apple decided to ignore such requests on mobile devices to prevent the browser from freezing.

Try with

xmlhttp.open("GET",path,true);

and a xhr.onload handler as described in MDN's "Synchronous and asynchronous requests".

Upvotes: 2

Related Questions