doulu1968 2013-09-11 19:50
浏览 60
已采纳

如何将ajax结果作为数组而不是字符串读取

Environment: PHP 5.3.5 MySQL Server 5.5.8

Created my first ever ajax call and after much determination I have got results. The results appear to be returning as a string. for when I request

alert(result[0]);

I am only returned a single character.

If I try to use a field name, I have a value of undefined returned.

Here is the javascript for my AJAX:

$.ajax({ 
    type: "POST", 
    url: "models/ajaxHandler.php", 
    data: "handler=this&stu=21", 
    success: function(result){ 
            if (result !== null) 
            { 
                    alert(result + " " + result[0] +" " result['firstname']);  
                    var obj =  JSON && JSON.parse(result) || $.parseJSON(result);
                    alert(obj + " " + obj[0] + " " + obj['firstname']);
            } 
            ShowDialog(false); 
            e.preventDefault(); 
    }, 
    error: function()
    { 
            alert("ajax failure"); 
    } 
});

So once the result is captured using a PDO connection in PHP to mysql with a fetchAll(). I return the results from the php like so:

echo json_encode($results);

The alerts for the results comeback like so:

[{"firstname":" Test","0":" Test","lastname":" One","1":" One","id":"2","2":"2","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Two","1":" Two","id":"3","2":"3","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Three","1":" Three","id":"5","2":"5","st_usage_id":null,"3":null}] [ undefined

The parse of the JSON returns all results as object.

I have also tried returning the results in php using:

print_r(json_encode($results));

It displays the same string above. Lastly I used

$var_dump(results);

This returned the following:

array(3) {
  [0]=>
  array(8) {
    ["firstname"]=>
    string(7) " DrTest"
    [0]=>
    string(7) " DrTest"
    ["lastname"]=>
    string(4) " One"
    [1]=>
    string(4) " One"
    ["user_public_info_id"]=>
    string(1) "2"
    [2]=>
    string(1) "2"
    ["st_usage_id"]=>
    NULL
    [3]=>
    NULL
  }
  [1]=>
  array(8) {
    ["firstname"]=>
    string(3) " Dr"
    [0]=>
    string(3) " Dr"
    ["lastname"]=>
    string(4) " Two"
    [1]=>
    string(4) " Two"
    ["user_public_info_id"]=>
    string(1) "3"
    [2]=>
    string(1) "3"
    ["st_usage_id"]=>
    NULL
    [3]=>
    NULL
  }
  [2]=>
  array(8) {
    ["firstname"]=>
    string(3) " Dr"
    [0]=>
    string(3) " Dr"
    ["lastname"]=>
    string(6) " Three"
    [1]=>
    string(6) " Three"
    ["user_public_info_id"]=>
    string(1) "5"
    [2]=>
    string(1) "5"
    ["st_usage_id"]=>
    NULL
    [3]=>
    NULL
  }
}
 a undefined

I am not sure what it is I am missing, and I'm sure it is something simple. I feel like I have tried everything. Please could someone tell me where I went wrong?

  • 写回答

4条回答 默认 最新

  • dsgsdg206050 2013-09-12 16:52
    关注

    In case you want to accept an answer:

    Try:

    alert(obj[0]['firstname']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?