繁体   English   中英

Ajax调用php-未定义索引错误

[英]Ajax call to php - Undefined index error

流程:主页-> php(tester.php)上的ajax请求-> json信息返回首页。

我找不到解决此错误的方法。 谢谢

阿贾克斯电话

$.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获取的信息已由AJAX调用返回,因此您只需从返回的data变量中检索即可。 您可以重写为以下内容:

 $.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){    
                response = $.parseJSON(data);                                       
                document.getElementById('userdensity').innerHTML = response[0]+","+response[1];
                document.getElementById('advertising_cost').innerHTML = response[2]+","+response[3];
            };
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM