我正在使用 AJAX 向 PHP 发送值并从 PHP 中检索值。问题是我从 PHP 获得的值在 AJAX 中被视为未定义。请帮我解决这个问题。
AJAX 代码:
var channel;
function overall() {
$(".one").show();
$(".two").hide();
$(".three").hide();
$(".four").hide();
window['channel'] = "OVERALL";
$.ajax({
type: "GET",
url: "dash2.php",
data: ({channel: channel}),
success: function (data) {
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
}
PHP代码:
<?php
$channel=$_GET['channel'];
$host="192.168.0.29";
$username="root";
$password="root";
$dbname="realcl";
mysql_connect($host,$username,$password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = 'select * from '.$channel;
$masterresult = mysql_query($query);
while($row1 = mysql_fetch_array($masterresult))
{
$success=$row1[1];
$timeout=$row1[2];
$fail=$row1[3];
}
echo json_encode(array("a"=>"$success","b"=>"$timeout","c"=>"$fail"));
?>