读取 JSON 要点作为查询字符串

Read a JSON gist as a query string

我目前有一个包含三个文件的 Javascript 应用程序:

index.html
app.js
input.json

input.json 文件被 app.js 文件多次引用,以便将内容呈现到 index.html 文件中的 div 中。

我想这样做,以便在 http://example.com/myapp 加载我的 index.html 可以将外部托管的 JSON 作为 app.js 阅读的来源,例如我的示例。 com/myapp?myhost.com/files/input.json.

任何人都可以告诉我如何让它工作吗?

为此,您需要在 myexample.com 上启用 CORS

假设您使用的是 jQuery。 (如果您不想对 ajax 请求使用 jQuery,请查看 here

$.get('http://myexample.com/myapp?myhost.com/files/input.json')
.done(function(data) {
    // use external 'data' here.
})
.fail(function() {
    // handle error here
});