gpt4 book ai didi

php - 为什么我的 xmlhtpp 请求返回一个空数组?

转载 作者:行者123 更新时间:2023-11-30 22:17:30 25 4
gpt4 key购买 nike

我正在使用以下请求从我的 data.php 文件中请求数据:

xmlhttp.send('request=getchartdata&'+'chart='+chart+'&'+'key='+key);

data.php 的内容如下:

if ($_POST["request"] == "getchartdata") {

/*Removing dash from key*/
$key = str_replace("-", "", $_POST["key"]);

if ($_POST["chart"] == "associationEvolutionSubventions") {
$result = $conn->prepare("SELECT grantYear, grantAmount FROM granttoassociation WHERE HEX(grantReceiver) = ? ");
/*grantReceiver is in binary*/
}

$result->execute(["{$key}"]);
while($rs = $result->fetch()) {
if ($outp != "") {
array_push($outp,$rs);
}
}
}

$outp = json_encode($outp);
echo($outp);

但是,我在 xmlhttp.responseText 中得到一个空数组。

与 MySQL 数据库的连接不是问题(另一个 xmlhtpp 请求正确返回数据)。不过,有几点我不确定我的代码:

  • $result->execute(["{$key}"]); 这里的语法正确吗?
  • HEX(grantReceiver) 如果 grantReceiver 是二进制的,这样做是否正确?
  • $key = str_replace("-", "", $_POST["key"]); 删除破折号的语法是否正确?

编辑:这里是请求的完整 AJAX 代码。

xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
};
xmlhttp.open("POST", "wp-content/plugins/mygaloochart/data.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send('request=getchartdata&'+'chart='+chart+'&'+'key='+key);

最佳答案

重新安排您的代码。打开请求并设置请求头后,你应该监听onreadystate(以下是你修改后的代码):

xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "wp-content/plugins/mygaloochart/data.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
};
xmlhttp.send('request=getchartdata&'+'chart='+chart+'&'+'key='+key);

关于php - 为什么我的 xmlhtpp 请求返回一个空数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37784207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com