流程:主页 - > php上的ajax请求(tester.php) - > json信息返回主页面。Ajax调用php - 未定义的索引错误
我无法找到解决此错误的方法。由于
Ajax调用
$.ajax({
url : "tester.php",
type : "POST",
//dataType : 'json',
data : {
'lat':lat,
'lng':lng,
'year1':year1,
'month1':month1,
'day1':day1,
'year2':year2,
'month2':month2,
'day2':day2,
'category':category
},
success: function(data)
{
$.getJSON('tester.php',function(cost)
{
document.getElementById('userdensity').innerHTML = cost[0]+","+cost[1];
document.getElementById('advertising_cost').innerHTML = cost[2]+","+cost[3];
});
});
PHP的:(tester.php):
<?
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$year1 = $_POST['year1'];
$year2 = $_POST['year2'];
$cost = array($lat,$lng,$year1,$year2);
echo json_encode($cost);
?>
错误:
[02-Mar-2015 21:02:35 Europe/Berlin] PHP Notice: Undefined index: lat in /Users/tester.php on line 2
[02-Mar-2015 21:02:35 Europe/Berlin] PHP Notice: Undefined index: lng in /Users/tester.php on line 3
[02-Mar-2015 21:02:35 Europe/Berlin] PHP Notice: Undefined index: year1 in /Users/tester.php on line 4
[02-Mar-2015 21:02:35 Europe/Berlin] PHP Notice: Undefined index: year2 in /Users/tester.php on line 5
不确定错误所在。我过去做过这件事,并且进展顺利。
解决方案:改变成功:
success: function(data)
{
var info = $.parseJSON(data);
document.getElementById('userdensity').innerHTML = info[0]+","+info[1];
document.getElementById('advertising_cost').innerHTML = info[2]+","+info[3];
}
在成功参数为什么你再次调用$ getJSON? – 2015-03-02 20:18:53
您的'成功'函数缺少关闭'}' – mhall 2015-03-02 20:24:01
是的错误发生,因为您使用$ .getJSON发送GET请求到tester.php。在第二个不必要的请求中没有可用的POST变量 – NaN 2015-03-02 20:24:51