doujingke4981
doujingke4981
2017-02-08 16:34

为什么jQuery AJAX。 返回值未定义?

  • json
  • php
  • ajax
  • javascript
  • jquery

A weird thing is happening when I execute a function that "alerts" data from a json. If I specify, like alert(data.name);, the "Alert" returns "Undefined", but if I just put alert(data);, it returns the object like {"id":"1","name":"Erluan"}

This is the function that receives an id to search in the database

function receiveUser(val){
            $.ajax({
               type:"POST",
               url:"../json/userperm/userGrid.php",
               data: 'iduser='+val,
               datatype:"json",
               success: function(data, string, jqXHR){
                    alert(data.name);
               }
            });
}

And this is the userGrid.php

<?php
include('../../config.php');

$user = mysql_query("SELECT * from hospital_".$_SESSION['template'].".users where id = ".$_POST['iduser']." order by name");

$results = array();

while($row = mysql_fetch_array($user))
{
   $results[] = array(
      'id' => $row['id'],
      'name' => $row['name']
   );
}

$json = json_encode($results);

echo $json;
?>

Thank you.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐