我正在尝试从数据库返回数据并将其转换为 json 并将其发送回我的 javascript。但是它没有按计划工作,它作为 javascript 上的数组返回。
Javascript
function getData(id) {
$.ajax({
url: 'some url',
crossDomain: true,
type: 'post',
data: {
id: id
},
success: function (data) {
var json = jQuery.parseJSON(data);
alert(data);
},
});
};
PHP
<?php
header("access-control-allow-origin: *");
$dbhost = 'you dont need this info';
$dbuser = 'you dont need this info';
$dbpass = 'you dont need this info';
$db = 'you dont need this info';
$dbserver = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db) or die("Unable to select database: " . mysql_error());
if (isset($_POST['id'])) {
$ID = $_POST['id'];
}
echo $ID;
$result = mysql_query('SELECT * FROM clubmember WHERE clubID = "' . $ID . '"');
$row = mysql_fetch_assoc($result);
$name = $row['name'];
$clubID = $row['clubID'];
$lID = $row['lID'];
$sName = $row['sName'];
$desc = $row['description'];
$json = json_encode(array(
'name' => $name,
'clubID' => $clubID,
'lID' => $lID,
'sName' => $sName,
'description' => $desc
));
echo $json;
?>
以 ...形式发出的 javascript 警报[object Object], [object Object]
不应该是这种情况...