尽管对此有很多问题,但我似乎无法找到适合我情况的代码,但每次运行此代码时,我都会得到一个未定义的变量,而不是所需的返回 JSON。
我的 AJAX 代码是:
$.ajax({
data: {"serial":account},
url: 'http://127.0.0.1/MobilePHP/findCustomerName.php',
dataType: 'json',
success: function(data){
window.alert(data);
}
});
我的PHP代码是:
<?php
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');
include 'dbConfig.php';
include 'connectDB.php';
//$account = $_POST['serial'];
$account = 14;
$sth = mysql_query("SELECT customer_name AS Name FROM customer_details WHERE auto_id = $account ");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows ['CustomerName'][] = $r;
}
echo json_encode($rows);
include 'closeDB.php';
?>
我的控制台中的 JSON 是:
{"CustomerName":[{"Name":"Tullaroan"}]}
我真的不确定为什么我无法访问这些变量,因为它似乎在控制台上返回了正确的 JSON。