小编典典

从php数组获取数据-AJAX-jQuery

json

我的页面如下:

<head>
<script type="text/javascript" src="jquery-1.6.1.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('#prev').click(function() {
  $.ajax({
  type: 'POST',
  url: 'ajax.php',
  data: 'id=testdata',
  cache: false,
  success: function(result) {
    $('#content1').html(result[0]);
  },
  });
});
});
</script>
</head>
<body>
<table>
<tr>
<td id="prev">prev</td>
<td id="content1">X</td>
<td id="next">next</td>
</tr>
</table>
</body>

和一个php文件ajax.php来处理ajax请求;

<?php
$array = array(1,2,3,4,5,6);
echo $array;
?>

但是,当我单击时,我得到的A不是array [0]。我怎样才能解决这个问题??

提前致谢…


阅读 824

收藏
2020-07-27

共1个答案

小编典典

您不能从js访问数组(php数组)try

<?php
$array = array(1,2,3,4,5,6);
echo json_encode($array);
?>

和js

$(document).ready( function() {
    $('#prev').click(function() {
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: 'id=testdata',
            dataType: 'json',
            cache: false,
            success: function(result) {
                $('#content1').html(result[0]);
            },
        });
    });
});
2020-07-27