javascript - 将混合数据类型数组从 php 传递到 javascript 数组

标签 javascript php mysql json getjson

这接近典型的异步/getJSON 主题。我似乎无法让我的工作正常...我正在 php 文件“loadblocks.php”中从 mySQL 数据库加载一个数组,并通过运行页面并执行 echo json_encode($内容);。所有数据都 100% 完美地打印到浏览器。但是,当我在 Javascript 中调用 json 数据时...一旦我单击带有 onclick 事件的链接,页面就没有响应。

<?php               
$con=mysqli_connect("localhost","root","password","swim");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM blocks");    
$content = array();     // initialize block viewer array    

if($result){
    while($row = mysqli_fetch_assoc($result)) {
        $content[$row["id"]] = $row;        // "id" is primary key id's
    }
}
echo json_encode($content);  // return array (validated)

mysqli_close($con);
?>

然后我的 HTML 文件中有以下内容...我需要从 mySQL 数据库加载这个“ block ”数据的多维数组。本质上,我需要在打开这个“ block 查看器”html/javascript 页面并将所有数据从 php 变量“$content”读取到多维数组中每一行的文字“ block ”中时执行此操作。每行包含一种教育类(class)要求、照片链接等...我的 HTML/jscript 如下:

<html>

<head>
<script>
    function loadblocks() {
      $("#selectable").selectable({
        disabled: true
      }); // disable selectable while re-loading blocks in "selectable" viewer
      $.getJSON('loadblocks.php', data, function(jsonarray) {
        // store jsonarray once it is loaded in here
        blockarray = jsonarray;
        // in MySQL each row = (id, blocktype, title, description, imgurl, img2url, img3url, vidurl, goal1, goal2, goal3)
        // Thus index #3 in each row is the "title" we will load in each box for now
        // todo - create loop to load all rows
        document.getElementById("selectable").innerHTML += "<li class='ui-state-default'>" + blockarray[0][3] + "</li>";
      })
      $(".selectable").selectable({disabled: false}); // re-enable
    }
  </script>
</head>

<body>

  <table class="floatboxstyle" style="width:675px; height:225px;">
    <tr>
      <td>
        <ol id="selectable">
          <li class="ui-state-default">Sample block</li>
        </ol>
      </td>
    </tr>
  </table>
  <button onclick="loadblocks()">Load Blocks</button>

  <br>

  <p id="feedback">
    <span>You've selected:</span>  <span id="select-result">none</span>
  </p>

</body>
</html>

最佳答案

您不必使用 parseJSON 进行响应,jsonData 应该已经是一个 JavaScript 对象,事实上这可能应该是一个错误,尽管我通常使用JSON.parse 所以我不确定 parseJSON 是否会爆炸。我没有看到您在此代码中使用 blockarray 执行任何操作。并且您没有用于报告问题的错误处理程序,请尝试添加一个。

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.[1]

[1] http://api.jquery.com/jQuery.getJSON/

此外,尽管您可能会在浏览器中看到 JSON 打印,但它可能不是有效的 JSON,在这种情况下,它会默默地失败。

As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason.

尝试验证您的 JSON 以确保它确实有效,尽管我假设 json_encode 会为您做到这一点。

关于javascript - 将混合数据类型数组从 php 传递到 javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172828/

相关文章:

mysql - 合并两个表并按 OrderBy 计数

mysql - 如何在 MySQL 中执行异常处理(参照完整性)

javascript - 来自设置对象的 Handsontable 自动完成源(对于每个单元格)

javascript - JavaScript 性能中的空数组测试

javascript - 来自 YouTube 视频帧的 WebGL 纹理

php - 这个正则表达式匹配什么?来自 mediawiki 源代码的 php 正则表达式

javascript - 如何使用 CreateJS 或其他 Javascript 解决方案创建栏动画

php - 对特定 MySQL 数据库中的整个表运行查询

php - 需要帮助查询竞赛获胜者的数据库

mysql - 转换为 unix 时间戳并再次返回会导致 MySQL 丢失 1 天