0

If i load a json file like this in HTML

<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"

{"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?

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>
  • getJSON expects url as its first parameter – Amit Joki Sep 13 '14 at 15:11
  • `$.getJSON("#id", function(person)` is not how you get the json. – AdityaParab Sep 13 '14 at 15:11
  • 1
    You load the URL with `$.getJSON()` (which essentially a shortcut for a GET request typed for a JSON response). Note, however, if the JSON is cross-domain (meaning coming from another site), you have to use [`jsonp`](http://api.jquery.com/jquery.getjson/#jsonp) ([more explanation here](http://stackoverflow.com/a/2067584/451969)). – Jared Farrish Sep 13 '14 at 15:14
  • And you can only use JSONP if the server supplying it can pass it to you in that format. – Andy Sep 13 '14 at 16:51
  • i cant is wel protectet! – user3329634 Sep 13 '14 at 17:21

1 Answers1

1

You're using getJSON wrong way. Check the official docs, getJSON expects URL.

$(window).load(function() {
    $.getJSON('json.json', function(person) {
        $.each(person, function(key, value) {
            document.write(key + '= ' + value + '<br />');
        });
    });
});
AdityaParab
  • 7,024
  • 3
  • 27
  • 40