JSON returns null


JSON returns null

我面临着通过getJSON获取变量值的问题。这是我正在尝试的代码
display.php

<html>
<body>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
            $.getJSON("code.php", function(data) {
      alert("Value for 'a': " + data.first + "'nValue for 'b': " + data.last);
    });
</script>
</body>                                                                                             
</html> 

code.php

<?php    
$var1=$_REQUEST['id'];
$var2="Some hard codded text";
$output =  array('first'=>$var1,
                 'last'=>$var2);
echo json_encode($output,JSON_PRETTY_PRINT);
?>

我使用alert(result)和getJSON来检查变量的值,alert(result)显示这两个值,但getJSON为ID值返回"null"。

调用$.getJSON时需要发送数据参数使用如下代码所示的添加数据参数:

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

$.ajax()启动一个ajax请求,并在响应中使用$.getJSON()启动另一个ajax请求。

您可以使用进行一次ajax调用

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

尝试

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