dongmei9961 2012-11-08 16:26
浏览 36
已采纳

Json用PHP mysql和Jquery

I have a strange problem..

When i try to access my json code with the corresponding number [0], [1] etc.. I just get the first character of the object.

First of my code:

test2.php

if(isset($_POST['getCustomersArray'])){
$runQuery = mysql_query($Query) or
        die("SQL: $Query)<br />".mysql_error()); 
$numrows = mysql_num_rows($runQuery); 
$array = array(array());

for($i = 0;$i <= 2; $i++){
    $row = mysql_fetch_array($runQuery);
    $array[$i]['namn'] = $row['fornamn'];

}
    print json_encode($array);
}

scriptfile.js

$.ajax({
    type:"POST",
    url: "test2.php",
    data: "getCustomersArray=true",
    datatype: "JSON",
    cache: false,
    success: function(json) {
            console.log(json[0]);
     }
});

The result (from console.log(json[0])):

[

The result from just console.log(json):

[{"namn":"the first name"},{"namn":"The secound name"},{"namn":"the third name"}]

Im not sure why the squarebrackets are there but maybe they should be?

Ive been fuzzing with this problem for a while now and im sure its something stupid. Please help.

  • 写回答

3条回答 默认 最新

  • duanniu3385 2012-11-08 16:28
    关注

    You have an incorrect option in the AJAX settings,

    datatype: "json",
    

    It should be:

    dataType: "json",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接