如何接收从php返回的json数据并使用?

解决方案 »

  1.   

    php中的代码if($_POST["tem_again"]){
    $tem=$_POST["tem_again"];
    global $cfg_dbhost , $cfg_dbuser , $cfg_dbpwd , $cfg_dbname , $cfg_me_dir;
    $server=mysql_connect($cfg_dbhost , $cfg_dbuser , $cfg_dbpwd);
    $connection=mysql_select_db($cfg_dbname , $server);

    $sql="select * from tem_info where tem='$tem'";
    $result=mysql_query($sql);
    $row=mysql_fetch_array($result);
    $json=json_encode($row);
    echo $json;
    }
      

  2.   

    需要指定返回值的类型:
    dataType:'json'
      

  3.   

    1 乱码原因, 是因为你的网页编码与数据库的编码不一致所造成的. 建议你在SELECT 之前, 先送出 SET NAMES '你的网页编码';2 重复的东西是因为 查询的时候, 数据库返回了 不带key 的数组与 带字段名key 的数组.  看 http://cn2.php.net/manual/en/function.mysql-fetch-array.php mysql-fetch-array 的第二个参数使用.3 返回的是一个JSON串, 你要当对象使用. 可以这样玩var json = eval(json);
    alert(json['qq']);