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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀