在我的应用程序运行时,我遇到了一些加载XML的问题。这段代码在浏览器中运行得非常好,所以我唯一能想到的就是我对phonegap deviceready
挂钩做错了。
任何想法都会非常感激。
我已经取出了xml的主要开发服务器地址,顺便说一句。
var xmlhttp;
window.onload = function() {
document.addEventListener('deviceready', getData, false);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = loadData;
}
function getData() {
xmlhttp.open("GET", "http://example.com/xml.php", true);
xmlhttp.send();
}
function loadData() {
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
var storiesXML = xmlhttp.responseXML.documentElement.getElementsByTagName("stories");
var wrapper = '<div class="storiesWrapper">';
var contentPage = '';
for (var i = 0; i < storiesXML.length; i++) {
var homeOutput = '<div class="storyContainer">';
var titleElement = storiesXML[i].getElementsByTagName("title");
var titleText = titleElement[0].firstChild.nodeValue;
var summaryElement = storiesXML[i].getElementsByTagName("summary");
var summaryText = summaryElement[0].firstChild.nodeValue;
var imgElement = storiesXML[i].getElementsByTagName("img");
var imgText = imgElement[0].firstChild.nodeValue;
var fulltextElement = storiesXML[i].getElementsByTagName("fulltext");
var fulltextText = fulltextElement[0].firstChild.nodeValue;
var videolinkElement = storiesXML[i].getElementsByTagName("videolink");
var videoLink = videolinkElement[0].firstChild.nodeValue;
var pageId = titleText.toLowerCase().replace(/\s+/g, '');
homeOutput += '<img class="thumbnail" src="' + imgText + '" />';
homeOutput += '<a class="titleText" data-transition="slide" href="#' + pageId + '">' + titleText + '</a>';
homeOutput += '<p class="summaryText">' + summaryText + '</p>';
homeOutput += '<div class="clear"></div>';
homeOutput += '</div>';
if ((document.getElementById(pageId)) == (null)) {
var pageOutput = '<div class="pageContent" data-role="page" data-theme="b" id="' + pageId + '">';
pageOutput += '<div data-role="header" class="header">';
pageOutput += '<a class="backButton" href="javascript:history.go(-1)" data-transition="slide" data-diraction="reverse" data-icon="back">Back</a>'
pageOutput += '<h1>Stories</h1>';
pageOutput += '</div>';
pageOutput += '<h2 class="storyTitle">' + titleText + '</h2>';
pageOutput += '<a href="' + videoLink + '">';
pageOutput += '<img class="featuredImage" src="' + imgText + '" />';
pageOutput += '</a>';
pageOutput += '<div data-role="content" class="mainText">';
pageOutput += fulltextText;
pageOutput += '</div>';
pageOutput += '</div>';
contentPage += pageOutput;
}
wrapper += homeOutput;
}
wrapper += '<div class="clear"></div>';
wrapper += '</div>';
document.getElementById("stories").innerHTML = wrapper;
if ((document.getElementById(pageId)) == (null)) {
$(contentPage).insertAfter("p#insertAfter");
}
}
}
setInterval(getData, 60000);
答案 0 :(得分:0)
似乎这个问题是window.onload
触发deviceready
我将代码更改为此,现在可以正常使用
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = loadData;
document.addEventListener('deviceready', getData, false);
function getData() {
xmlhttp.open("GET", "http://dev.emagpress.com/app-dev/helloxml/appxml.php", true);
xmlhttp.send();
}