我正在尝试加载我使用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!