简体   繁体   中英

Ajax call to php - Undefined index error

Flow: main page -> ajax request on php(tester.php) -> json information back to main page.

I can't find a way around this error. Thanks

ajax call

$.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);

?>

Error:

[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

Unsure where the error is. I have done this in the past and went well.

Solution : changed success to:

success: function(data)
                {
                    var info = $.parseJSON(data);
                            document.getElementById('userdensity').innerHTML = info[0]+","+info[1];
                            document.getElementById('advertising_cost').innerHTML = info[2]+","+info[3];
                }

The information you're trying to get using getJson is already being returned by the AJAX call, so you can simply retrieve from the data variable returned. You can rewrite to something like this:

 $.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];
            };
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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