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

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

问题描述

我在服务器中生成的这个JSON文件是我想要在客户端上访问的,因为页面是可见的。基本上我想达到的是:

我有以下内容:

 < script id =testtype =application / jsonsrc =http:/myresources/stuf.json> 

标签在我的html文档中声明。源码中引用的文件具有json数据。正如我所看到的,数据已经被下载,就像脚本一样。

现在,我如何在JavaScript中访问它?我尝试过使用和不使用jQuery访问脚本标记,使用多种方法尝试获取我的json数据,但不知何故,这不起作用。如果将json数据以内联方式写入脚本,那么获得它的innerHTML就可以奏效。这不是,也不是我想要实现的。



在页面加载后的远程JSON请求也不是一种选择,以防您想要提示。

解决方案

您无法像加载JSON一样加载JSON,



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

 < script id =myJsontype =application / json> 
{
名称:'Foo'
}
< / script>

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

...简而言之,就是脚本标记被滥用为数据持有者。你可以用各种数据来做到这一点。例如,大量模板引擎利用脚本标签来保存模板



从远程文件加载JSON的选项列表很短:


  1. 使用 $。get('your.json')或者其他一些这样的AJAX方法。
  2. 编写一个为全局变量设置json的文件。 (看起来很乱)。

  3. 将它拉到一个不可见的iframe中,然后在加载后将其内容擦掉(我称之为1997模式)

  4. 咨询voodoo牧师。

最后一点:


加载页面后的远程JSON请求也不是一个选项,以防您想要提示。


..这没有意义。 AJAX请求与浏览器在处理< script src => 时发送的请求之间的区别本质上是没有的。他们都会在资源上进行GET。 HTTP并不关心它是由于脚本标记还是AJAX调用而完成的,您的服务器也不会。


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:

I have the following:

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

tag declared in my html document. The file refered in its source has json data. As I've seen, data has been downloaded, just like it happens with the scripts.

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.

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

解决方案

You can't load JSON like that, sorry.

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.

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

  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.

Final point:

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

... 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天全站免登陆