Rajeet Rajeet - 1 year ago 66
Javascript Question

JSON returns null

I am facing the problem in getting the variable value through getJSON. Here is the code the i am trying!


<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
data: {id:102},
success: function(result){
alert(result); // this alert shows the proper result included the values of id.
$.getJSON("code.php", function(data) {
alert("Value for 'a': " + data.first + "\nValue for 'b': " + data.last);



$var2="Some hard codded text";
$output = array('first'=>$var1,
echo json_encode($output,JSON_PRETTY_PRINT);

I am using alert(result) as well as the getJSON to check the value of the variable, alert(result) shows both the values but the getJSON returns "null" for ID value.

Answer Source

The data parameter needs to be sent while calling $.getJSON Use add data parameter as shown below code:

 $.getJSON("code.php", data, function(data) {
  alert("Value for 'a': " + data.first + "\nValue for 'b': " + data.last);