I'm trying to parse a JSON response, but any instruction in the $.getJSON cannot be executed.
The json.html
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.js"></script>
</head>
<body>
<script>
$.getJSON("json.php?jsoncallback=?", function(data) {
var a = data[0].cve_id + 'something';
});
alert(a); //chrome says "a is not defined"
</script>
</body>
</html>
The json.php:
<?php
header("Content-Type: application/json", true);
echo file_get_contents(".php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfileinc=1&opgpriv=1&opsqli=1&opxss=1&opdirt=1&opmemc=1&ophttprs=1&opbyp=1&opginf=1&opdos=1&orderby=1&cvssscoremin=0");
?>
Any idea why?
LATER:
Thanks for the replies. I've understood the reason.
I've switched from $.getJSON
to $.get
, specifying the data type
to json
.
I'm trying to parse a JSON response, but any instruction in the $.getJSON cannot be executed.
The json.html
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery.js"></script>
</head>
<body>
<script>
$.getJSON("json.php?jsoncallback=?", function(data) {
var a = data[0].cve_id + 'something';
});
alert(a); //chrome says "a is not defined"
</script>
</body>
</html>
The json.php:
<?php
header("Content-Type: application/json", true);
echo file_get_contents("http://www.cvedetails./json-feed.php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=1&opec=1&opov=1&opcsrf=1&opfileinc=1&opgpriv=1&opsqli=1&opxss=1&opdirt=1&opmemc=1&ophttprs=1&opbyp=1&opginf=1&opdos=1&orderby=1&cvssscoremin=0");
?>
Any idea why?
LATER:
Thanks for the replies. I've understood the reason.
I've switched from $.getJSON
to $.get
, specifying the data type
to json
.
- 1 Your AJAX is asynchronous. Variables have scope. – I Hate Lazy Commented Nov 23, 2012 at 1:17
-
1
At the time
alert(a)
happens, the asynchronous AJAX call has not yet pleted. You need toalert(a)
inside thefunction(data)
callback. If you need to use the value to modify your DOM, you must do so in the callback – Michael Berkowski Commented Nov 23, 2012 at 1:18 -
2
chrome says "a is not defined" - That's because
a
is defined as a local variable inside the callback function that you pass to$.getJSON()
, so it can only be accessed inside that function (even aside from the async issue). – nnnnnn Commented Nov 23, 2012 at 1:20 -
I've also tried
alert(a)
insidefunction(data)
and the alert doesen't pop up. Tried alsoconsole.log(a)
and nothing shows up. – Schutzstaffel Commented Nov 23, 2012 at 1:23 -
If
alert(a)
inside the function doesn't work that suggests an error on the line before, e.g., ifdata
isn't an array with a0
element thendata[0]
isundefined
and doesn't have acve_id
property. Please show the JSON structure that is retrieved. (Tryconsole.log(data)
as the first line of the function...) – nnnnnn Commented Nov 23, 2012 at 1:28
2 Answers
Reset to default 2var a is defined inside the callback closure (anonymous function). One of the main reasons to use closures is that they produce private scope. I.e. any variables defined inside the function will not available to code outside of the function.
This will work:
$.getJSON("json.php?jsoncallback=?", function(data) {
var a = data[0].cve_id + 'something';
alert(a);
});
the JSON call is asynchronous so the callback function dont get dont get called until youre sever returns the data, But the javascript code keeps running to the next line of code. resulting in a stil being undefined try changing it to somthing along the lines of:
$.getJSON("json.php?jsoncallback=?", function(data) {
var a = data[0].cve_id + 'something';
alert(a)
});
you can define other methods outside of the callback and invoke em when the event happens whith the propper data