页面准备就绪时,将数据从本地文件存储到变量 [英] Store data from local file to variable when the page is ready

查看:59
本文介绍了页面准备就绪时,将数据从本地文件存储到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var persons;
        $(function () {
            $.getJSON("person.json", function(data){ persons = JSON.stringify(data); 
                console.log("This is inside: "+ persons); });
                console.log("This is outside" + persons);
            }
            getJSONData();
        });





i想要将本地文件(person.json)中的数据存储到开头的变量中。但结果是:



i want to get and store data from local file(person.json) into a variable at the beginning. But the result is:

This is outside: undefined
This is inside: [{"name":"user1","gender":"male"},{"name":"user2"},{"gender":"female"}]





我怎么能在一开始就将数据存储到变量中?



我尝试过:





So how can i get and store data into variable at the very beginning?

What I have tried:

var persons;
    function getJSONData() {
        $.getJSON("person.json", function(data){ persons = JSON.stringify(data); 
        console.log("This is inside: "+ persons); });
    }
    getJSONData();
    $(function () {
        console.log("This is outside: " + persons);
    });



我试过这个,但结果是一样的......


I have tried this, but the result is same...

推荐答案

function (){


.getJSON( person.json function (data){persons = JSON .stringify(data);
< span class =code-sdkkeyword> console .log( 这里面是: +人);});
console .log( 这是 +人)之外;
}
getJSONData();
});
.getJSON("person.json", function(data){ persons = JSON.stringify(data); console.log("This is inside: "+ persons); }); console.log("This is outside" + persons); } getJSONData(); });





i想要将本地文件(person.json)中的数据存储到变量中。但结果是:



i want to get and store data from local file(person.json) into a variable at the beginning. But the result is:

This is outside: undefined
This is inside: [{"name":"user1","gender":"male"},{"name":"user2"},{"gender":"female"}]





我怎么能在一开始就将数据存储到变量中?



我尝试过:





So how can i get and store data into variable at the very beginning?

What I have tried:

var persons;
    function getJSONData() {


.getJSON( person.json function (data){persons = JSON .stringify(data);
console .log( 这里面是: + persons);});
}
getJSONData();
.getJSON("person.json", function(data){ persons = JSON.stringify(data); console.log("This is inside: "+ persons); }); } getJSONData();


这篇关于页面准备就绪时,将数据从本地文件存储到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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