简体   繁体   English

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

[英]Passing mixed data type array from php to javascript array

This is close to the typical asyncronous / getJSON topics. 这接近于典型的异步/ getJSON主题。 I can't seem to get mine to work... I am loading an array from mySQL database in a php file "loadblocks.php" and have tested its output to work just fine by running the page and doing an echo json_encode($content);. 我似乎无法正常工作...我正在从MySQL数据库中的php文件“ loadblocks.php”中加载数组,并通过运行页面并执行echo json_encode($测试了其输出是否正常内容);。 All the data prints to the browser 100% perfect. 所有数据都可以100%完美地打印到浏览器。 However, when I am calling the json data in Javascript... the page is just not responding once I click the link with my onclick event. 但是,当我用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);
?>

Then I have the following in my HTML file... I need to load this multi-dimensional array of "block" data from the mySQL database. 然后,我的HTML文件中包含以下内容...我需要从mySQL数据库中加载“块”数据的多维数组。 Essentially I need to do this when opening this "block viewer" html/javascript page and read all the data from the php variable "$content" into a literal "block" for each row in the multi-dimensional array. 从本质上讲,当打开此“块查看器” html / javascript页面并将多维数组中的每一行的php变量“ $ content”中的所有数据读入文字“块”时,我都需要这样做。 Each row contains a sort of educational curriculum requirement, a photo link, etc. etc... My HTML/jscript is as follows: 每行都包含某种教育课程要求,照片链接等,等等。我的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>

You don't have to use parseJSON for the response, jsonData should already be a JavaScript object, in fact that should probably be an error, although I usually use JSON.parse so I'm not sure if parseJSON blows up or not. 您不必使用parseJSON进行响应,尽管我通常使用JSON.parse ,但jsonData应该已经是一个JavaScript对象,实际上这应该是一个错误,因此我不确定parseJSON是否会parseJSON I don't see you doing anything with blockarray in this code. 我看不到您在此代码中使用blockarray做任何事情。 And you don't have an error handler to report problems, try adding one. 而且您没有错误处理程序来报告问题,请尝试添加一个。

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. 成功回调将传递返回的数据,该数据通常是由JSON结构定义并使用$ .parseJSON()方法进行解析的JavaScript对象或数组。 It is also passed the text status of the response.[1] 还传递了响应的文本状态。[1]

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

Also though you may be seeing the JSON print with the browser, it may not be valid JSON in which case it would fail silently. 同样,尽管您可能会在浏览器中看到JSON打印,但它可能不是有效的JSON,在这种情况下,它将以静默方式失败。

As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. 从jQuery 1.4开始,如果JSON文件包含语法错误,则请求通常会静默失败。 Avoid frequent hand-editing of JSON data for this reason. 因此,请避免对JSON数据进行频繁的手动编辑。

Try and validate your JSON to make sure it is actually valid, although I'd assume json_encode would do that for you. 尝试验证您的JSON以确保它实际上有效,尽管我认为json_encode会为您做到这一点。

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

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