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>
5
  • getJSON expects url as its first parameter Commented Sep 13, 2014 at 15:11
  • $.getJSON("#id", function(person) is not how you get the json. Commented Sep 13, 2014 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 (more explanation here). Commented Sep 13, 2014 at 15:14
  • And you can only use JSONP if the server supplying it can pass it to you in that format. Commented Sep 13, 2014 at 16:51
  • i cant is wel protectet! Commented Sep 13, 2014 at 17:21

1 Answer 1

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 />');
        });
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

yes i know this but the json is on another domain, and is protected from being opened like this!
You'll have to use jsonp in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.