$ .jquery .post将数据(json)返回为'undefined'

时间:2014-02-02 13:49:14

标签: javascript php jquery json

遇到了这个问题,真的不知道为什么要显示未定义:((请帮我解决一下!谢谢!!!

<script>$( "select" ).change(function() {
  var name = $('select#usersfromhell').val();
  $.post('dynamicselect.php', {name: name}, function(data) {

      var jsonString =JSON.stringify(data);
      var obj = JSON.parse(jsonString);
      alert(obj.jmeno); // here shows undefined!!!!
      $('input#name').val(jsonString); // but here correctly fill the input value --- "{\"jmeno\":\"http:\\/\\/www.jobs.cz\"}"
  });
});</script>

dynamicselect.php

$users->get('users', array('username', '=', $_POST['name']));

foreach($users->results() as $user) {

    //echo $user->name ." ". $user->joined . " " . $user->group;
    echo json_encode(array("jmeno" => $user->name)); // "{\"jmeno\":\"http:\\/\\/www.jobs.cz\"}"
}

谢谢!

1 个答案:

答案 0 :(得分:2)

您正在使用JSON.stringify,因为您从php

发送json数据,这不是必需的

删除 var jsonString = JSON.stringify(data);