ramislav
ramislav

Reputation: 21

$.jquery .post returned data (json) as 'undefined'

Got this problem and really dunno why shows undefined :(( Please help me figure it out! Thanks!!!

<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\"}"
}

Thanks!

Upvotes: 0

Views: 170

Answers (1)

Learner
Learner

Reputation: 231

you are Using JSON.stringify which is not required as you are sending json data from php

Remove var jsonString =JSON.stringify(data);

Upvotes: 2

Related Questions