我在尝试将结果从PHP脚本返回到HTML / Jquery应用程序时遇到了问题。
目前,我正在尝试连接服务器,如下所示:
function getDatabaseRows()
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
xmlHttp.open("POST", "myDomain.com/subDirectory/getRowCounts.php", true);
xmlHttp.send(null);
}我的响应处理程序是:
function HandleResponse(response)
{
alert(response.length);
if(response.length != 0)
maxArray = JSON.parse(response);
storage.set('currentID', maxArray);
}目前,每当我运行代码时,getDatabaseRows()总是以调用HandleResponse(response)结束。问题是response.length总是等于0。
PHP脚本只是从数据库中收集最大行数并返回行数:
<?php
$rowCounts = [];
$dbhost = 'host';
$dbuser = 'host';
$dbpass = 'host';
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db("host") or die(mysql_error());
$sql = "SELECT MAX(TID) AS MaxTID FROM TransData";
$results = mysql_query($sql, $con);
$transRow = mysql_fetch_array($results);
$maxTrans = $transRow['MaxTID'];
$sql = "SELECT MAX(FID) AS MaxFID FROM FullData";
$results = mysql_query($sql, $con);
$fullRow = mysql_fetch_array($results);
$maxFull = $fullRow['MaxFID'];
$sql = "SELECT MAX(SID) AS MaxSID FROM SalamanderData";
$results = mysql_query($sql, $con);
$salamanderRow = mysql_fetch_array($results);
$maxSal = $salamanderRow['MaxSID'];
$sql = "SELECT MAX(OID) AS MaxOID FROM OthersData";
$results = mysql_query($sql, $con);
$othersRow = mysql_fetch_array($results);
$maxOther = $othersRow['MaxOID'];
array_push($rowCounts, $maxTrans, $maxFull, $maxSal, $maxOther);
echo json_encode($rowCounts);
mysql_close($con);
?>当我将浏览器直接指向脚本时,echo显示我的行数是正确的。我只是在尝试通过AJAX获取数据时遇到了问题。
编辑:
好了,我终于把脚本连接起来了,它跟跨域脚本问题有关。现在一切都解决了。
然而,新的问题:
function HandleResponse(response)
{
if(response.length != 0)
maxArray = JSON.parse(response);
storage.set('currentID', maxArray);
}在解析JSON时,HandleResponse抛出了一个错误,所以我认为这与Error trying to parse '<'的编码方式有关。我该如何解决这个问题并弄清楚如何解析该数组呢?
发布于 2012-09-13 04:06:38
我将检查以下连接方法。
http://api.jquery.com/jQuery.get/
这将解决所有问题并清理您的代码
这是我的一个旧代码的示例
$.ajax({
data: "id=" + value,
type: "POST",
url: "unlock.php",
success: function (data) {
if (data == 1 || data == true) {
var result = true;
} else {
var result = false;
}
}
});发布于 2012-09-13 05:22:02
这是我能得到的最简单的jQuery JavaScript (使用get方法):
function getDatabaseRows()
{
$.getJSON('myDomain.com/subDirectory/getRowCounts.php', function(data) {
console.log(data);
});
}http://api.jquery.com/jQuery.getJSON/上的文档
以及使用foreach {}和mysql_result()对PHP进行的一些增强,尽管您可能希望研究PHP的mysqli类以获得更面向未来的解决方案。
$rowCounts = array();
$dbhost = 'host';
$dbuser = 'host';
$dbpass = 'host';
$dbase = 'host';
$fields = array(
'MaxTID' => array('tab' => 'TransData', 'col' => 'TID'),
'MaxSID' => array('tab' => 'FullData', 'col' => 'SID'),
'MaxFID' => array('tab' => 'SalamanderData', 'col' => 'FID'),
'MaxOID' => array('tab' => 'OthersData', 'col' => 'OID')
);
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbase, $con) or die(mysql_error());
foreach ($fields as $id => $info) {
$sql = "SELECT MAX({$info['col']}) FROM {$info['tab']}";
$result = mysql_query($sql, $con);
if (!$result) die('Could not query:' . mysql_error());
$rowCounts[$id] = mysql_result($result, 0);
}
echo json_encode($rowCounts);
mysql_close($con);这段代码在GoDaddy托管站点上运行。根据记录,这是我的第一个Stack Overflow答案。请告知:)
https://stackoverflow.com/questions/12395471
复制相似问题