简体   繁体   English

json作为javascript代码加载并使用变量

[英]json load as javascript code and use variables

If i load a json file like this in HTML 如果我在HTML中加载这样的json文件

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" id="json" src="json.json"></script>

The json file looks like this and is from another domain and can't be opened by "$getJson" json文件如下所示,并且来自另一个域,无法通过“ $ getJson”打开

{"name":"Stop","instance":"Find","quality":"port","vsm":"port1","smn":"port2","Protection":"UK","fail":"oun domain","restriction":"other domain"}

Is there any way to use the variables from the json file in a JavaScript code? 有没有办法在JavaScript代码中使用json文件中的变量?

I've tried this but no luck! 我已经尝试过了,但是没有运气!

<script type='text/javascript'>
$(window).load(function(){
$.getJSON("#id", function(person){
$.each(person, function(key, value)
{document.write(key+"= "+value+"<br />"); 
});
});
});
</script>

You're using getJSON wrong way. 您使用的是错误的getJSON方法。 Check the official docs , getJSON expects URL. 检查官方文档getJSON需要URL。

$(window).load(function() {
    $.getJSON('json.json', function(person) {
        $.each(person, function(key, value) {
            document.write(key + '= ' + value + '<br />');
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM