简体   繁体   English

无法使用getJSON从php接收JSON

[英]not receiving JSON from php with getJSON

I am trying to pass an array to the browser using php and jquery but I the when I try to use the 'data' returned from php's encode_json, it comes up undefined. 我正在尝试使用php和jquery将数组传递给浏览器,但是当我尝试使用从php的encode_json返回的“数据”时,它未定义。 I'm just learning php, jquery, and json and so far haven't found very good documentation on alot of this stuff, especially json, even in the books I have. 我只是在学习php,jquery和json,到目前为止,即使在我拥有的书中,也没有找到很多关于这些东西的很好的文档,尤其是json。 Thanks in advance! 提前致谢!

Here is a stripped down version of the jquery I have 这是我拥有的jQuery的精简版本

$(document).ready(function(){
    var jsonResult;//I will want to be able to use the data in other functions

    $.getJSON("json.php", function(data){  
        jsonResult = data;
        var str;
        var nuts = [203,204,205,207];

        str = '<p>' + data[nuts[0]].NutraDesc + '</p>';
        $('#stuff').html(str);
        }
    );
});

This is the php: 这是PHP:

    include_once 'databasePHP.php';
        $json_tst = $db->query( "SELECT  def.Nutr_No, NutrDesc, Nutr_Val, Units 
                FROM nutr_def as def  JOIN nut_data as data  ON def.Nutr_No = data.Nutr_No 
                WHERE data.NDB_No = 1001 LIMIT 0, 2");

        $food = array();

        while($row = $json_tst->fetch(PDO::FETCH_ASSOC))
        {
           $Nutr_No = $row['Nutr_No'];
           $food[$Nutr_No][] = array(
               'NutrDesc' => $row['NutrDesc'],
               'Nutr_Val' => $row['Nutr_Val'],
               'Units' => $row['Units']
           );
        };

        echo  json_encode($food);
?>

which returns this json which I checked on jsonlint.com and it said it was valid: 返回我在jsonlint.com上检查过的json,并说它是有效的:

 {"203":[{"NutrDesc":"Protein","Nutr_Val":"0.85","Units":"g"}],"204":[{"NutrDesc":"Total lipid (fat)","Nutr_Val":"81.11","Units":"g"}]}

It probably doesn't work because the numbers should be strings. 可能不起作用,因为数字应该是字符串。 Try to add quotes around the numbers in nuts, like this: 尝试在螺母中的数字周围加上引号,如下所示:

var nuts = ["203","204","205","207"];

The following probably works as well: 以下可能也适用:

str = '<p>' + data[String(nuts[0])].NutraDesc + '</p>';

Also, have you tried adding console.log(data); 另外,您是否尝试过添加console.log(data); to the getJSON function to make sure it receives the JSON? 到getJSON函数以确保它接收到JSON?

EDIT: Here is a working JSFiddle from your code: http://jsfiddle.net/rKLqM/ 编辑:这是从您的代码工作JSFiddle: http : //jsfiddle.net/rKLqM/

Things that were wrong: 错误的事情:

  • you weren't parsing the result as JSON ( JSON.parse ) 您没有将结果解析为JSON( JSON.parse
  • NutraDesc was spelled wrong NutraDesc拼写错误
  • You didn't convert the numbers to strings 您没有将数字转换为字符串
  • You needed to add [0] to the jsonResult because there's an extra array within it (see the []) 您需要将[0]添加到jsonResult中,因为其中包含一个额外的数组(请参见[])

In Javascript object property can be accessed with obj["propName"] 在Javascript对象中,可以使用obj["propName"]访问属性

So, change 所以,改变

var nuts = [203,204,205,207];

to

var nuts = ["203","204","205","207"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM