XMLHttpRequest在Google Chrome Packaged Web App中不起作用

XMLHttpRequest not working in Google Chrome Packaged Web App

本文关键字:App 不起作用 Web Packaged Google Chrome XMLHttpRequest      更新时间:2023-09-26

我正在编写一个打包的Chrome Web应用程序,并尝试解析外部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文件中,我根据谷歌对跨源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"

}

我不确定我是否还遗漏了什么,但代码对我不起作用。有什么建议、技巧或想法吗?我真的很感激任何帮助!!

只需在/后面加一个通配符即可。否则,您将允许您的扩展仅在根目录下请求页面。

"permissions": [
  "http://www.w3schools.com/*",

在您的情况下,您只请求一个URL。对模式进行更多限制可能会更好:

"permissions": [
  "http://www.w3schools.com/xml/note.xml",