Download local content via XHR in a Chrome app

I am trying to upload to a web application that I created using Backbone and it downloads JSON and HTML template files that are stored locally. I was wondering, with the help of Chrome packages, is it possible to download these files using some kind of "get" / ajax request?

I am currently 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 not find any real information on how to do this, so any help would be greatly appreciated!

+5
source share
3 answers

, , . . , , , . , 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!
+6

, .

Google.

Chrome "", , XHR API- Chrome, ./p >

, .

EDIT:

answer Chrome CORS *.

+2

, , . jQuery :

Access-Control-Allow-Origin: *

, . ID , - :

Access-Control-Allow-Origin: chrome-extension://gmelhokjkebpmoejhcelmnopijabmobf/

- , :

<h1>Content Below</h1>
<div id="loadme"></div>
<script src="jquery-1.9.1.min.js"></script>
<script src="app.js"></script>

// app.js
$(document).ready(function() {
  $.get('http://localhost:8080/content.php', function(data) {
    $('#loadme').html(data);
  });
});

, Access-Control-Allow-Origin:

XMLHttpRequest cannot load http://localhost:8080/newhope/deleteme.php.
Origin chrome-extension://gmelhokjkebpmoejhcelgkfeijabmobf is not allowed by
Access-Control-Allow-Origin. 

Access-Control-Allow-Origin php, .

Again, setting this parameter to *may pose a security risk, since any browser page anywhere can load it into a string.

0
source

All Articles