問(wèn)題描述
我正在嘗試加載使用 Backbone 構(gòu)建的 Web 應(yīng)用程序,它會(huì)提取本地存儲(chǔ)的 JSON 和 HTML 模板文件.我想知道 Chrome 打包應(yīng)用是否可以通過(guò)使用某種get"/ajax 請(qǐng)求來(lái)加載這些文件?
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?
目前我正在接受這個(gè)......
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.
我找不到任何有關(guān)如何執(zhí)行此操作的真實(shí)信息,因此非常感謝您的幫助!
I can't find any real information on how to do this so any help would be great thanks!
推薦答案
是的,這完全有可能,而且很簡(jiǎn)單.這是一個(gè)工作示例.嘗試從這個(gè)開(kāi)始,確認(rèn)它有效,然后添加回您自己的代碼.如果您遇到障礙并想出一個(gè)比 XHR 是否適用于打包應(yīng)用程序更具體的問(wèn)題,您可能想問(wèn)一個(gè)新問(wèn)題.
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:
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();
};
內(nèi)容.txt:
Hello, world!
這篇關(guān)于在 Chrome 打包應(yīng)用程序中通過(guò) XHR 加載本地內(nèi)容的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!