dt4233 2013-06-15 18:30
浏览 49
已采纳

如何在javascript中访问此关联数组?

I have a php file where I am pulling data from a database and I want to access its contents in javascript. When I try to access the array with data[0].card_id I get "undefined".

Here is my javascript

$(document).ready(function() {
  var userId = 1;
  var updateUrl;

  $.ajax({
    type: "POST",
    url: "url",
    data: {userId: userId},
    success: function(data) {
      alert(data[0].card_id);
      var suffix = ".html";
      fb.start('../Animations/' + updateUrl[0].card_id + suffix); 
    }
  });
}

Here is my php file

<?php

include('connect.php');

$user_id = $_POST['userId'];

$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL";
}

$select = "SELECT card_id FROM decks WHERE id=$user_id ORDER BY order_num";
$result = mysqli_query($db, $select);

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    $animation[] = array(
        'card_id' => $row['card_id'],
    );
}

json_encode($animation);
echo $animation;
mysqli_close($db);
?>

The array contains the following data

Array ( [0] => eating [1] => mummy

etc.. )

  • 写回答

2条回答 默认 最新

  • dpntq48842 2013-06-15 18:34
    关注

    You have two problems.

    First: You aren't doing anything with the return value of json_encode.

    Second: The PHP is claiming it is sending back HTML, so it wouldn't parsed as JSON anyway.

    header("Content-Type: application/json");
    echo json_encode($animation);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记