-3

イメージを含むテーブルからデータを取得し、JSON オブジェクトに格納する単純な PHP コードがあります。

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$selected = mysql_select_db("req2",$con)
or die("Could not select req2");
$table_first = 'locationtab';
$query=mysql_query("SELECT Id,City,Image FROM $table_first");

    while ($row=mysql_fetch_assoc($query)) {
        $array=$row;
        $array['Image']=base64_encode($row['Image']); 
       $output[]=$array;
    }

echo json_encode($output);

mysql_close($con);
?> 

これは問題なく動作し、print を使用して JSON オブジェクトを表示できます。クライアント側では、この PHP ファイルを呼び出す Javascript + Ajax コードを作成し、成功すると JSON オブジェクトにデータを表示します。以下のコードに何が欠けているのかわかりませんが、AJAX 部分がまったく機能していないようです。助けてください

<html>
<head>
<script src="file://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){ 
$.ajax({
     url:"table.php",
     dataType:'json',
     type:'POST',
      success:function(output) {
alert( output.Id );
               }
});

});
</script>
</head>
<body>
<p>test</p>
</body>
</html>
4

1 に答える 1

0

outputID を持つオブジェクトの配列です。

IDを持つ単一のオブジェクトとして扱っています。

forループするか、特定のインデックスを取得する必要があります。

alert( output[0].Id );
于 2013-01-15T07:18:38.453 に答える