1

How can I get the content of url.json, and cast the json to a variable. The content of url.json is pure json format. Thanx

4
  • Do you try to access a JSONP service? Commented Feb 9, 2012 at 10:19
  • 4
    You can't. It will be evaluated as JavaScript and just vanish in the void. Commented Feb 9, 2012 at 10:21
  • JSON is not a scripting language, so I guess it's not possible this way. Commented Feb 9, 2012 at 10:26
  • same-orgin, or cross domain? Commented Feb 8, 2013 at 22:48

3 Answers 3

0

if you use jquery

<script type="text/javascript">
    var json_variable;
    $.getJSON('url.json', function(json){
     json_variable = json;
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Are you aware of the jquery getJSON api? You can use this to download json from an url. Not sure if you can embed some json directly in a script tag, though.

Comments

0

You need to read in the contents from the url using AJAX and then put that into your variable or use the data as soon as you successfully get the data.

See here for non jQuery vanilla javascript methods: reading server file with javascript and also Consuming JSON data without jQuery (sans getJSON)

Comments

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.