从php传递json数组

时间:2016-11-07 12:21:43

标签: javascript php jquery json ajax

我花了几个小时搜索我的问题的解决方案,但我找不到答案。

我在同一页面上进行搜索,为此,我使用的是Jquery,AJAX和PHP。

php中的数组仍然返回undefined。

PHP代码:

$i=0;        
while($eee = mysqli_fetch_array($result)) { 
    $array[$i][1] = $eee['ex'];
    $array[$i][2] = $eee['ex'];
    $array[$i][3] = $eee['ex'];
    $array[$i][4] = $eee['ex'];
    $array[$i][5] = $eee['ex'];
    $array[$i][6] = $eee['ex'];
    $array[$i][7] = $eee['ex'];
    $i = $i + 1;
}
json_encode($array)

AJAX和JQUERY

jQuery(document).ready(function(){
      $(document).on('submit', '#FormData', function(e) {
            $.ajax({
                url: $(this).attr('action'),
                type: $(this).attr('method'),
                data: $(this).serialize(),
                success: function(data) {
                    console.log(data[1]);
                }
            }); 
            e.preventDefault();
        });
    });

感谢您的帮助;)

1 个答案:

答案 0 :(得分:-1)

在你的php文件中使用echo json_encode($array);

你的jquery中的

 type: 'json',
method:'post'