如何从JSON读取包含数组的json文件

时间:2018-10-26 11:23:21

标签: javascript php json ajax

请大家帮帮我,因为我找不到我能为我的javascript读取的json文件,其中包含一个包含一个元素的数组。

我的php文件运行正常,并且输出是一个包含以下行的.json文件:{"posts":[["30"]]}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

$sql= "SELECT site_id FROM components WHERE user_id='$user_id' && book_id='$book_id' && game_id='$game_id' ORDER BY site_id DESC LIMIT 1"; 
$response = array();
$posts = array();
$result=mysqli_query($link, $sql);
while($row=mysqli_fetch_assoc($result)) { 
  $site_id=$row['site_id'];
  $posts[] = array($site_id);
} 
$response['posts'] = $posts;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

// Close connection
mysqli_close($link);
?>

有人可以帮助我做我的事情(不使用Ajax)以便我的javascript函数读取该值吗?我想收回这个值,因为我想操纵这个数字。

    function load3() {
          var flag1 = true;
          do{
            var selection = window.prompt("Give the User Id:", "Type a number!");
                if ( /^[0-9]+$/.test(selection)) {
                flag1=false;
                }
            }
            while(flag1!=false);
                $("#user_id").val(selection)

                var flag2 = true;
            do{
                var selection2 = window.prompt("Give the Book Id:", "Type a number!");
                if ( /^[0-9]+$/.test(selection2)) {
                   flag2=false;
                }
            }
            while(flag2!=false);
                $("#book_id").val(selection2)

                var flag3= true;
            do{
                var selection3 = window.prompt("Give the Game Id:", "Type a number!");
                if ( /^[0-9]+$/.test(selection3)) {
                    flag3=false;
                }
            }
            while(flag3!=false);
                $("#game_id").val(selection3)

               //i do not want to do with ajax!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!            
                $.ajax({
                   type: 'POST',
                   url: 'http://127.0.0.1/PHP/loo.php',
                   data: $('#LoadGame').serialize(),
                   success: function (html) {
                      //do something on success?
                      $('#outPut').html(html);
                      var bingoValue=4;
                      if( $('#outPut').text().indexOf(''+bingoValue) > 0){
                         //alert('bingo!');
                         window.location.href='https://support.wwf.org.uk/';

               //document.location.replace('https://developer.mozilla.org/en-US/docs/Web/API/Location.reload');
                     }
                     else {
                        alert('No!');
                  }
              }
          });
      }

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

假设此PHP代码在您的文档请求期间运行,

如果将json放在脚本标签中,则可以读取该信息

<script type="text/javascript">
    window.myJson = <?php echo(json_encode($response)); ?>
</script

,它将在前端作为window.myJson访问