这看起来很简单,但我对此很陌生,以至于我无法让它运行——即使在阅读了数小时的教程和示例之后。我希望我的 PHP 运行一个查询,创建一个字符串数组,并将该数组返回给调用数据的 jQuery ajax()。返回数组应该是存储在数据库中的字符串,看起来像
citiesWithBoroughs = [
"Dallas, Dallas County, Texas, United States",
"Fort Worth, Tarrant County, Texas, United States",
"El Paso, El Paso County, Texas, United States"
];
查询和数组创建代码如下。该查询在 MySQL Workbench 中运行并返回我想要的数据。我在下面对国家和州进行了硬编码,以减少测试时的错误来源。当我进行受控测试时,我将用“mysql_real_escape_string($countryAbbreviation)”等替换它们。
$return_arr=array();
$fetch = mysql_query("SELECT DISTINCT cityWithBoroughsGeonamed FROM places.boroughs
WHERE countryAbbreviation='US'
AND stateProvinceAbbreviation='NY'
") or die(mysql_error());
while ($row = mysql_fetch_array($fetch)){
$return_arr[]=$row['cityWithBoroughsGeonamed'];
}
echo $return_arr;
目前,Firebug 告诉我键/值数据进入 PHP 服务器,但我得到一个空数组返回。我已经尝试了几件事,但没有得到任何东西或一个空数组 - 经过大量试验。
在 JavaScript 方面,这里是请求数据并获取返回数组的 ajax 函数。
citiesWithBoroughs = function (request, response){
$.ajax({
url: "getCitiesWithBoroughs.php",
data: {stateProvinceAbbreviation: $("#hiddenState").val(),
countryAbbreviation: $('input[name=country]:checked').val()},
type: "POST",
dataType: "text",
success: function (data){
response;
}
});
必须改变什么才能做到这一点?请具体说明代码。我对此很陌生。