Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON和Javascript有问题吗_Javascript_Jquery_Json - Fatal编程技术网

JSON和Javascript有问题吗

JSON和Javascript有问题吗,javascript,jquery,json,Javascript,Jquery,Json,我试图为我的客户项目制作一个简单的示例预览器。 样本列表存储在JSON文件中。但问题是,老实说,我不太熟悉JSON的使用或编程本身。 我已经编写了一个简短的测试代码,看看这个JSON文件是否运行良好,但是尽管我可以从firebug控制台访问sampleData变量,document.writesampleData.length;由于某种原因,line似乎无法访问sampleData。错误显示: TypeError:sampleData未定义 我怀疑这与变量作用域有关,但我不确定 <scri

我试图为我的客户项目制作一个简单的示例预览器。 样本列表存储在JSON文件中。但问题是,老实说,我不太熟悉JSON的使用或编程本身。 我已经编写了一个简短的测试代码,看看这个JSON文件是否运行良好,但是尽管我可以从firebug控制台访问sampleData变量,document.writesampleData.length;由于某种原因,line似乎无法访问sampleData。错误显示:

TypeError:sampleData未定义

我怀疑这与变量作用域有关,但我不确定

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    var sampleData;
    $.getJSON('res/Samples.json',function(data){
        sampleData=data;
    });
    document.write(sampleData.length);
</script>

据我所知,这是一种非常通用的JSON文件形式。

这是由于来自$.getJSON的异步响应

这基本上与:

var sampleData;
setTimeout(function() {
  sampleData = 'Some data'; // This is occurring after the document.write
}, 100);
document.write(sampleData.length);
您可以将document.write移动到响应处理程序中,但如下所述,它确实有:


这是由于来自$.getJSON的异步响应造成的

这基本上与:

var sampleData;
setTimeout(function() {
  sampleData = 'Some data'; // This is occurring after the document.write
}, 100);
document.write(sampleData.length);
您可以将document.write移动到响应处理程序中,但如下所述,它确实有:


它是异步发生的,因此您需要在异步调用本身中调用document.write:

var sampleData;
$.getJSON('res/Samples.json',function(data){
    sampleData = data;
    document.write(sampleData.length);
});

另外,如果您使用document.write在生产代码中写入页面,我建议您不要这样做。如果您在上面的示例中使用document.write只是为了调试,以找出它不起作用的原因,我建议改用console.log。更简单。

它是异步进行的,因此您需要调用document.write,在异步调用中写入:

var sampleData;
$.getJSON('res/Samples.json',function(data){
    sampleData = data;
    document.write(sampleData.length);
});

另外,如果您使用document.write在生产代码中写入页面,我建议您不要这样做。如果您在上面的示例中使用document.write只是为了调试,以找出它不起作用的原因,我建议改用console.log。更简单。

原因是在ajax调用返回放置文档所需的值之前调用sampleData.length;在ajax函数内部

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    var sampleData;
    $.getJSON('res/Samples.json',function(data){
        sampleData=data;
        document.write(sampleData.length);
    });

</script>

原因是在ajax调用返回放置文档所需的值之前调用sampleData.length;在ajax函数内部

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    var sampleData;
    $.getJSON('res/Samples.json',function(data){
        sampleData=data;
        document.write(sampleData.length);
    });

</script>
$.getJSON是一个异步函数,执行document.writesampleData.length;,sampleData尚未填充

您必须执行以下操作:

$.getJSON('res/Samples.json',function(data){
   sampleData=data;
   document.write(sampleData.length);
});
$.getJSON是一个异步函数,执行document.writesampleData.length;,sampleData尚未填充

您必须执行以下操作:

$.getJSON('res/Samples.json',function(data){
   sampleData=data;
   document.write(sampleData.length);
});
代码很好,但是$.getJSON只是开始获取JSON文件的过程,然后允许代码继续,调用document.write。稍后,在document.write调用之后,将调用对$.getJSON的回调。相反,在回调中触发要触发的任何后续代码

在这种情况下,无法有效地使用document.write。不过,您可以使用DOM和jQuery的各种DOM包装函数。例如:

$.getJSON('res/Samples.json',function(data){
    $("<p>").html(data.length).appendTo(document.body);
});
代码很好,但是$.getJSON只是开始获取JSON文件的过程,然后允许代码继续,调用document.write。稍后,在document.write调用之后,将调用对$.getJSON的回调。相反,在回调中触发要触发的任何后续代码

在这种情况下,无法有效地使用document.write。不过,您可以使用DOM和jQuery的各种DOM包装函数。例如:

$.getJSON('res/Samples.json',function(data){
    $("<p>").html(data.length).appendTo(document.body);
});

您可以将document.write移动到响应处理程序:您可以,但它会删除页面内容,并将其替换为该数字。初始解析后,使用document.write隐式调用document.open,删除页面。True,但这是另一个问题。了解事物如何运作的最好方法是观察它们是如何破裂的谢谢你指出文件。写东西。在我回到这篇文章之前,我试过了,但我不得不用艰苦的方式学习……你可以移动文档。写信给响应处理程序:你可以,但它会删除页面内容,并用这个数字替换。初始解析后,使用document.write隐式调用document.open,删除页面。True,但这是另一个问题。了解事物如何运作的最好方法是观察它们是如何破裂的谢谢你指出文件。写东西。在我回到这篇文章之前,我试过了,但是我不得不用艰苦的方式来学习……谢谢你的回答。我最终使用了带有异步参数的ajax语句……我知道这有点违背了ajax的含义,但不管怎样@K.H.:更重要的是,当请求进行时,它会锁定浏览器的UI,导致用户体验不佳。如果可能的话,接受异步性:但我很高兴这有帮助。谢谢你的回答。我最终使用了带有异步参数的ajax语句……我知道这有点违背了ajax的含义,但不管怎样@K.H.:更重要的是,当请求进行时,它会锁定浏览器的UI,导致用户体验不佳。如果可能的话 ,拥抱异步性。:-但我很高兴这有帮助。