加载在Chrome浏览器打包应用程序通过XHR本地内容 [英] Loading local content through XHR in a Chrome packaged app

查看:199
本文介绍了加载在Chrome浏览器打包应用程序通过XHR本地内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个Web应用程序使用我的骨干已经建立加载和它拉在存储在本地JSON和HTML模板文件。我想知道用浏览器打包应用程序是否有可能通过某种'得到'/ Ajax请求加载这些文件?

I'm trying to load in a web app that I've built using Backbone and it pulls in JSON and HTML template files that are stored locally. I was wondering with Chrome packaged apps whether it's possible to load these files by using some sort of 'get'/ajax request?

目前我得到这个...

Currently I'm getting this...

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.

我无法找到如何做到这一点,因此任何帮助将是巨大的感谢任何真实的信息!

I can't find any real information on how to do this so any help would be great thanks!

推荐答案

是的,这是完全可能的,而且很容易。这里的工作示例。尝试从这个,确认它的工作原理,然后再重新添加在自己的code。如果碰到一个障碍,并拿出一个比XHRs无论是在打包应用程序的工作更具体的问题,你可能要问一个新问题。

Yes, it's totally possible, and it's easy. Here's a working sample. Try starting with this, confirm that it works, and then add back in your own code. If you hit a roadblock and come up with a more specific question than whether XHRs work in packaged apps, you might want to ask a new question.

的manifest.json:

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:

background.js:

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

window.html:

window.html:

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

main.js

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:

content.txt:

Hello, world!

这篇关于加载在Chrome浏览器打包应用程序通过XHR本地内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆