dongzhan5246 2016-10-09 17:08
浏览 172
已采纳

json_encode返回未定义

So I've been reading up on a few guides on using json to return values to JS from php. I've however encountered a bit of a problem. Right now, I've got a jquery-function (don't get distracted with 'id: 1', it's used for something else), such as:

$('#some_trigger').on('change', function(){
    $.post("retrieve.data.php", {
            id: 1
      }, function (data) {
          var values = data;

          alert(values[name]);
    });
});

And on the receiving end (in 'retrieve.data.php'), I've got:

$arr_assoc_template = array(
"name" => "aName",
"id" => "anId"
);

echo json_encode($arr_assoc_template, JSON_PRETTY_PRINT);

However, the console and alert window keeps logging [name] as undefined. What am I missing? :(

  • 写回答

2条回答 默认 最新

  • dongnao6858 2016-10-09 17:10
    关注

    Use JSON.parse, turns a string of JSON text into a Javascript object.

    var values = $.parseJSON(data);
    
    console.log(values.name);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应