[英]Ajax response undefined or strange
i'm trying to send an array to JS, but i can't have the answer i want. 我正在尝试向JS发送数组,但我无法得到我想要的答案。
this is my PHP code: 这是我的PHP代码:
$output = array('total'=>(float)$BHoras[1]'gastas'=>(float)$BHoras[2]);
echo json_encode($output);
and this is my JS code: 这是我的JS代码:
function ProjectSelect()
{
var proj = document.getElementById('ProjetosSelect').value;
$.ajax({
url: 'CRM files/TSread.php',
type: "POST",
data: ({ProjetosSelect: proj}),
complete:function(data)
{
var Horas = data.responseText;
alert(Horas); // response -> {"total":146,"gastas":84.5}
alert(Horas[3]); // response -> o
}
});
}
i only want the "146" and "84.5". 我只想要“146”和“84.5”。
i tried to do, alert(Horas['total'])
, alert(Horas.total)
, but give me undefined
我试着做,
alert(Horas['total'])
, alert(Horas.total)
,但是给我undefined
Just specify dataType: "json"
and jQuery will parse response for you: 只需指定
dataType: "json"
,jQuery将为您解析响应:
function ProjectSelect()
{
var proj = $('#ProjetosSelect').val();
$.ajax({
url: 'CRM files/TSread.php',
type: "POST",
data: ({ProjetosSelect: proj}),
dataType: "json",
success: function(Horas)
{
alert(Horas.total);
}
});
}
On server side you could try TracKer note. 在服务器端,您可以尝试TracKer note。 And you can add a header too.
你也可以添加一个标题。
<?php
$output = array('total'=>(float)$BHoras[1], 'gastas'=>(float)$BHoras[2]);
header('Content-type: application/json');
echo json_encode($output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.