dongshan4518 2015-04-05 15:13
浏览 47
已采纳

JSON解析没有从数组中获取值[关闭]

I'm executing a JSON parse on an array returned from a PHP function and it doesn't seem to be working.

Here's the PHP function:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php

            $bname = $_REQUEST["bname"];

            $link = mysqli_connect('localhost', 'root', '123'); 

            $servername = "localhost";
            $username = "root";
            $password = "123";
            $dbname = "success";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
            // PHP for execution
            $sql = "SELECT id, bname, bicon, rafrica, rasia, roceania, reurope, rsouthamerica, rnorthamerica, traffic, revenue, profit FROM business LIMIT 1";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    $b3name = $row["bname"]. "<br>";
                    $b3icon = $row["bicon"]. "";
                    $b3rafrica = $row["rafrica"]. "<br>";
                    $b3rasia = $row["rasia"]. "<br>";
                    $b3roceania = $row["roceania"]. "<br>";
                    $b3reurope = $row["reurope"]. "<br>";
                    $b3rsouthamerica = $row["rsouthamerica"]. "<br>";
                    $b3rnorthamerica = $row["rnorthamerica"]. "<br>";
                    $b3traffic = $row["traffic"]. "<br>";
                    $b3revenue = $row["revenue"]. "<br>";
                    $b3profit = $row["profit"]. "<br>";
                }
            } else {
                echo "0 results";
            }

            $output = array(
                'name' => $b3name,
                'icon' => $b3icon,
                'traffic' => $b3traffic
            );
            
            echo json_encode($output);

?>
</body>
</html>

Here's the AJAX that contains the JSON parse:

        function loadfacebook1()
        {
            var xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("b1").innerHTML=xmlhttp.responseText;
                }
              } 
            
            xmlhttp.open("GET","getfacebook.php",true);
            xmlhttp.send();

            var obj = JSON.parse(xmlhttp.responseText);
            document.getElementById("demo").innerHTML=obj.name + "<br>";
        }

I'm using

<span id="demo">

to display the returned value but I need to assign obj.name (and some other elements of the array) to a variable(s) which I can use to update other things in the page. Any help would be really appreciated.

Cheers,

Will

</div>
  • 写回答

2条回答 默认 最新

  • dphj737575 2015-04-05 15:18
    关注

    You should move the parsing of received JSON into the function called when the AJAX response is delivered (onreadystatechange)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面