微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax – 在Chrome打包应用中通过XHR加载本地内容

我正在尝试加载我使用Backbone构建的Web应用程序,它会提取本地存储的JSON和HTML模板文件.我想知道Chrome打包应用程序是否可以通过使用某种’get’/ ajax请求加载这些文件

目前我正在接受这个……

OPTIONS chrome-extension://fibpcbellfjkmapljkjdlpgencmekhco/templates/templates.html Cannot make any requests from null. jquery.min.js:2
XMLHttpRequest cannot load chrome-extension://fibpcbellfjkmapljkjdlpgencmekhco/templates/templates.html. Cannot make any requests from null.

我找不到任何关于如何做到这一点的真实信息,所以任何帮助都会非常感谢!

是的,这是完全可能的,而且很容易.这是一个工作样本.尝试从此开始,确认它有效,然后在您自己的代码添加回来.如果您遇到障碍并提出一个更具体的问题,而不是XHR是否在打包应用中运行,您可能想问一个新问题.

manifest.json的:

{
  "name": "SO 15977151 for EggCup","description": "Demonstrates local XHR","manifest_version" : 2,"version" : "0.1","app" : {
    "background" : {
      "scripts" : ["background.js"]
    }
  },"permissions" : []
}

background.js:

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create("window.html",{ bounds: { width: 600,height: 400 }});
});

window.html:

<html>
<body>
  <div>The content is "<span id="content"/>"</div>
  <script src="main.js"></script>
</body>
</html>

main.js:

function requestListener() {
  document.querySelector("#content").innerHTML = this.responseText;
};

onload = function() {
  var request = new XMLHttpRequest();
  request.onload = requestListener;
  request.open("GET","content.txt",true);
  request.send();
};

content.txt:

Hello,world!

原文地址:https://www.jb51.cc/ajax/160223.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐