AJAX 成功调用仅从 JSON 字符串中获取一个值
Posted
技术标签:
【中文标题】AJAX 成功调用仅从 JSON 字符串中获取一个值【英文标题】:AJAX success call taking only one value from JSON string 【发布时间】:2020-09-08 23:23:34 【问题描述】:我使用 json_encode 从 php 文件传递一个数组到 $.ajax 调用。当我检查 PHP 的输出时,它显示值被正确传递,但在 ajax 调用中它只接受第一个键值,其余变为 0 或 null。请帮忙。这是我的代码:
HTML 代码:
<label class="control-label">Date Range</label>
<div class="col-md-4">
<div class="input-group input-large date-picker input-daterange" data-date="10/11/2012" data-date-format="yyyy-mm-dd">
<input id="frmdate" type="text" class="form-control" name="from">
<span class="input-group-addon"> to </span>
<input id="todate" type="text" class="form-control" name="to"> </div>
<button type="button" onClick="get_val();" class="btn grey-mint">Submit</button>
</div>
<div class="tile double-down">
<div class="tile-body">
<div id="tot_whouse">
</div>
</div>
<div class="tile-object">
<div style="text-align: center;">Total Whouses </div>
</div>
</div>
<div class="tile double-down">
<div class="tile-body">
<div id="vis_whouse"> </div>
</div>
<div class="tile-object">
<div style="text-align: center;">Whouse Visited </div>
</div>
</div>
<div class="tile double">
<div class="tile-body">
<div id="ad_dfs"> </div>
</div>
<div class="tile-object">
<div class="name"> % Whouse having adequate Stock </div>
</div>
</div>
PHP 代码:
$indi = array();
$dtfrom = $_REQUEST['dtfrm']; // From Date
$dtto = $_REQUEST['dateto']; // To Date
//Total Whouse
$sql = "SELECT count(ID) FROM `W_Mast` WHERE S_I=3";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result);
$totwe=$row[0];
$indi['tot_whouse']=$totwe;
// Whouse Visited
$sql = "SELECT count(wm.ID) FROM `WHouseMonitor` wm INNER JOIN W_Mast w on wm.WhouseID=w.WHouse_ID WHERE w.S_I=3 and wm.visit_date BETWEEN $dtfrom and $dtto";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result);
$w_visited = $row[0]; //Will be used as Denominator in next query result
$indi['vis_whouse']=$w_visited;
//Whouse have stock
$sql = "SELECT count(wm.ID) FROM `WHouseMonitor` wm INNER JOIN W_Mast w on wm.WhouseID=w.WHouse_ID WHERE w.S_I=3 and wm.dfs_open>=dfs_suppquar and wm.visit_date BETWEEN $dtfrom and $dtto";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result);
$w_adeqdfs= round((($row[0]/$w_visited)*100),1);
$indi['adeq_dfs']=strval($w_adeqdfs);
echo json_encode($indi);
通过url传递参数检查这个PHP的结果-
https://website.com/whouse.php?dtfrm="2019-01-01"&dateto="2040-12-31"
"tot_whouse":"210","vis_whouse":"8","adeq_dfs":"100"
带有 $.ajax 脚本的 JavaScript:
<script>
var dtfrom;
var dtto;
function get_val()
dtfrom = document.getElementById("frmdate").value;
dtto = document.getElementById("todate").value;
if(dtfrom=="")
dtfrom ="2019-01-01";
if(dtto=="")
dtto ="2040-12-31";
$.ajax(
url: "whouse.php",
type: 'POST',
dataType: 'json',
data:
dtfrm: dtfrom,
dateto: dtto
,
success: function(response)
$("#tot_whouse").html(response['tot_whouse']);
$("#vis_whouse").html(response['vis_whouse']);
$("#ad_dfs").html(response['adeq_dfs'] + '%');
,
error: function(e, ts, et)
alert(ts);
alert(et);
);
</script>
$.ajax 响应变量正在接收以下值:
"tot_whouse":"210","vis_whouse":"0","adeq_dfs":"NAN"
关于它不采用 "vis_whouse" 和 "adeq_dfs" 值的任何想法。
谢谢!
【问题讨论】:
假设正如您所展示的那样收到响应,那么理论上您的代码没有任何问题 - 演示 jsfiddle.net/e738h9rg 。也许您的选择器名称错误或其他什么?我们看不到目标 HTML。尝试在“成功”中将这些单独的值记录到控制台,看看它是否打印出来。然后你就会知道是数据有问题还是选择器有问题。vis_whouse
和ad_dfs
是输入框还是div?
嗨@ADyson,选择器的名称是正确的,response 收到的值已打印在各自的 div 中。我已经在控制台中记录了这些值,结果如下:tot_whouse: "210", vis_whouse: "0", adeq_dfs: "NAN"
adeq_dfs: "NAN" tot_whouse: "210" vis_whouse: "0"
@Swati vis_whouse 和 ad_dfs 是 div。
@Dyson 我发现了问题!正如您正确提到的,问题确实出在 PHP 上。日期应该作为字符串值传递。我通过在双码中传递 url 中的日期来获取 PHP 输出。我已经将从 AJAX 接收到的日期转换为字符串,并且它有效!感谢您提供有关 PHP/mysqli 的提示,我一定会遵循。谢谢!
【参考方案1】:
我已经解决了问题!传递给 PHP 的日期应该是字符串格式。所以我所做的就是在我的 SQL 查询中将日期转换为字符串,现在我的 AJAX 正在接收正确的响应。
之前我的 sql 查询是:
$sql = "SELECT count(wm.ID) FROM `WHouseMonitor` wm INNER JOIN W_Mast w on wm.WhouseID=w.WHouse_ID WHERE w.S_I=3 and wm.visit_date BETWEEN $dtfrom and $dtto";
我现在改为:
$sql = "SELECT count(wm.ID) FROM `WHouseMonitor` wm INNER JOIN W_Mast w on wm.WhouseID=w.WHouse_ID WHERE w.S_I=3 and wm.visit_date BETWEEN '".$dtfrom."' and '".$dtto."'";
感谢大家的帮助!
【讨论】:
以上是关于AJAX 成功调用仅从 JSON 字符串中获取一个值的主要内容,如果未能解决你的问题,请参考以下文章
jQuery Ajax 调用返回 JSON 字符串而不是对象数组
如何在 AJAX 成功函数中调用 element.onload?