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
Asked
Active
Viewed 5,067 times
1
-
Do you try to access a JSONP service? – Fox32 Feb 09 '12 at 10:19
-
4You can't. It will be evaluated as JavaScript and just vanish in the void. – Felix Kling Feb 09 '12 at 10:21
-
JSON is not a scripting language, so I guess it's not possible this way. – pimvdb Feb 09 '12 at 10:26
-
same-orgin, or cross domain? – Joshua Wooward Feb 08 '13 at 22:48
3 Answers
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)

Community
- 1
- 1

Anthony Hatzopoulos
- 10,437
- 2
- 40
- 57
0
if you use jquery
<script type="text/javascript">
var json_variable;
$.getJSON('url.json', function(json){
json_variable = json;
});
</script>

James Lin
- 25,028
- 36
- 133
- 233
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.

Thibault J
- 4,336
- 33
- 44