【发布时间】:2012-07-07 17:03:46
【问题描述】:
我正在编写一个打包的 Chrome Web App 并尝试解析外部 XML。当我将 XML 文件下载到我的服务器时它可以工作,但当我将 XML 文件更改为 Web 上的位置时它不起作用。
例如,我正在尝试访问这个文件:http://www.w3schools.com/xml/note.xml
这是我的 JavaScript 代码(注意 HTML ):
function dashboardContent() {
html="";
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://www.w3schools.com/xml/note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
html+=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
html+=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
html+=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
document.getElementById("contentArea").innerHTML=html;
}
在我的 Chrome Web App manifest.json 文件中,我将以下内容置于 Google 的 manifest requirement for Cross-origin XMLHttpRequests 权限之下:
{
"name": "BirdTrax",
"version": "0.1.1",
"description": "Birding explorer to help you plan birding trips and find recently eBirded sightings, rarities, and checklists in your area",
"icons": {
"16": "icons/helloWorld_16.png",
"128": "icons/helloWorld_128.png"
},
"app": {
"launch": {
"local_path": "main.html",
"container": "tab"
}
},
"background_page": "background.html",
"options_page": "options.html",
"permissions": [
"http://www.w3schools.com/*",
"notifications",
"unlimitedStorage"
],
"incognito": "split"
}
我不确定我是否仍然缺少某些东西,但代码对我不起作用。有什么建议、提示、想法吗?我真的很感谢任何帮助!
【问题讨论】:
-
该脚本放在哪个文件中?函数是怎么调用的?
-
@RobW 该函数在 元素加载时调用。 中链接的脚本作为默认第一页加载。
-
我想我会复制 Google 自己的示例代码并尝试从那里开始工作。感谢您的帮助!
标签: javascript xml json google-chrome xmlhttprequest