HTML/Javascript:如何使用 src 集访问脚本标签中加载的 JSON 数据 [英] HTML/Javascript: how to access JSON data loaded in a script tag with src set

查看:31
本文介绍了HTML/Javascript:如何使用 src 集访问脚本标签中加载的 JSON 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器中生成了这个 JSON 文件,我想让它在客户端上可以访问,因为页面是可见的.基本上我想要实现的是:

I have this JSON file I generate in the server I want to make accessible on the client as the page is viewable. Basically what I want to achieve is:

我在我的 html 文档中声明了以下标签:

I have the following tag declared in my html document:

<script id="test" type="application/json" src="http://myresources/stuf.json">

其来源中引用的文件具有 JSON 数据.正如我所见,数据已下载,就像脚本中发生的一样.

The file referred in its source has JSON data. As I've seen, data has been downloaded, just like it happens with the scripts.

现在,我如何在 Javascript 中访问它?我试过使用 jQuery 和不使用 jQuery 访问脚本标记,使用多种方法来尝试获取我的 JSON 数据,但不知何故这不起作用.如果将 json 数据内联写入脚本中,获取它的 innerHTML 会起作用.它不是,也不是我想要实现的目标.

Now, how do I access it in Javascript? I've tried accessing the script tag, with and without jQuery, using a multitude of methods to try to get my JSON data, but somehow this doesn't work. Getting its innerHTML would have worked had the json data been written inline in the script. Which it wasn't and isn't what I'm trying to achieve.

页面加载后的远程 JSON 请求也不是一个选项,以防您想提出建议.

Remote JSON Request after page loads is also not an option, in case you want to suggest that.

推荐答案

你不能那样加载 JSON,抱歉.

You can't load JSON like that, sorry.

我知道你在想为什么我不能在这里使用 src?我见过这样的东西......":

I know you're thinking "why I can't I just use src here? I've seen stuff like this...":

<script id="myJson" type="application/json">
 { 
   name: 'Foo' 
 }
</script>

<script type="text/javascript">
    $(function() {
        var x = JSON.parse($('#myJson').html());
        alert(x.name); //Foo
     });
</script>

... 简单地说,这只是脚本标签被滥用"为数据持有者.你可以用各种数据来做到这一点.例如,很多模板引擎利用脚本标签来保存模板.

... well to put it simply, that was just the script tag being "abused" as a data holder. You can do that with all sorts of data. For example, a lot of templating engines leverage script tags to hold templates.

您有一个从远程文件加载 JSON 的简短选项列表:

You have a short list of options to load your JSON from a remote file:

  1. 使用 $.get('your.json') 或其他类似的 AJAX 方法.
  2. 编写一个文件,为您的 json 设置一个全局变量.(看起来很傻).
  3. 将其拉入一个不可见的 iframe,然后在加载后抓取其中的内容(我称之为1997 模式")
  4. 请教巫毒教牧师.
  1. Use $.get('your.json') or some other such AJAX method.
  2. Write a file that sets a global variable to your json. (seems hokey).
  3. Pull it into an invisible iframe, then scrape the contents of that after it's loaded (I call this "1997 mode")
  4. Consult a voodoo priest.

最后一点:

页面加载后的远程 JSON 请求也不是一种选择,以防万一您想提出建议.

Remote JSON Request after page loads is also not an option, in case you want to suggest that.

...这没有意义.AJAX 请求和浏览器在处理您的 <script src=""> 时发送的请求之间的区别本质上没有什么区别.他们都会对资源执行 GET.HTTP 不关心它是否因为脚本标记或 AJAX 调用而完成,您的服务器也不会.

... that doesn't make sense. The difference between an AJAX request and a request sent by the browser while processing your <script src=""> is essentially nothing. They'll both be doing a GET on the resource. HTTP doesn't care if it's done because of a script tag or an AJAX call, and neither will your server.

这篇关于HTML/Javascript:如何使用 src 集访问脚本标签中加载的 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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