AJAX中未定义的值

时间:2012-03-16 02:29:31

标签: javascript ajax

我在下面有一个函数,使用google maps api存储两个位置的距离。这些结果存储在volunteerDist中,并且有一个定义的jid数组。第5行中的Document.write显示了数组中每个元素的内容。现在,当我提醒志愿者之一的Object.prototype时,就是说对象数组。但是当我调用$ .ajax并将volunteerDist转移到数据并提醒它时(如下所示),它返回undefined和success(意味着数据存储但内容未定义)。有帮助吗?感谢

function getABC(){
for(s=0;s<length;s++){
    volunteerlocation = new GLatLng(jlat[s], jlng[s]);
    volunteerDist[s] = (Math.round((eventlocation.distanceFrom(volunteerlocation) / 1000)*10)/10);
    document.write(volunteerDist[s] + '<br> ');
    document.write(jid[s] + '<br> ');
}

alert(Object.prototype.toString.call(volunteerDist));

 $.ajax({
    type:'POST',
    url: 'toDistance.php',
    data : ({
        distance:volunteerDist,
        id:jid
    }),
    success: function(data){
         alert(data);
         alert('worked');
    },
   error :function(jqXHR, textStatus, errorThrown) {
        alert(errorThrown);
    },
   complete : function(){
       alert('thanks');
   }
});
}

更新

以下是我的toDistance.php

<?php
    $distance=array();
    $volunteerid=array();
    if(empty($_GET)){
        echo "Hello";
    }
    else{

        $distance = isset($_GET['distance']) ? $_GET['distance'] : 0;
        $volunteerid = isset($_GET['id']) ? $_GET['id'] : 0;
        $connect = mysql_connect("localhost","root","");
        mysql_select_db("mapping");


        for($i=0;$i<$distance.length;$i++){
            $updateDistance = mysql_query("
            UPDATE volunteerbio
            SET volunteerDistance = $distance[$i]
            WHERE volunteerID = $volunteerid[$i];
            ");
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

现在它是相同的数据变量!它在其他范围内。

成功函数中的data是服务器返回的内容。如果它让你困惑,请给它别的名字。

$.ajax({
    type:'POST',
    url: 'toDistance.php',
    data : ({
        distance:volunteerDist,
        id:jid
    }),
    success: function(result){ // Data sent from the server. not the data above!
         alert(result);
         alert('worked');
    },
   error :function(jqXHR, textStatus, errorThrown) {
        alert(errorThrown);
    },
   complete : function(){
       alert('thanks');
   }
});