2012-03-16 84 views
1

我有一個函數,它使用google maps API存儲兩個位置的距離。那些結果存儲在volunteerDist中,並且有一個定義好的jid數組。第5行中的Document.write顯示了數組中每個元素的內容。現在,當我提醒志願者Dist的Object.prototype ...時,說對象Array。但是當我調用$ .ajax並將志願者數據轉化爲數據並提醒它時(如下所示),它將返回未定義的成功(意味着數據已存儲但內容未定義)。任何幫助?由於未定義AJAX中的值

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

這是現在同樣的數據變量!它在其他範圍內。

成功函數中的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'); 
    } 
}); 
+0

它返回一個空白警報..什麼是錯的? – 2012-03-16 02:34:32

+0

@BrandonYoung。我不知道,這個腳本沒有錯。你可能想用服務器代碼打開一個新的問題線程。 **這個腳本很好!** – gdoron 2012-03-16 02:37:07

+0

我編輯了我的問題並添加到了Distance.php中,我注意到我在toDistance.php的第5行中迴應的內容是「結果」中的提醒。所以這種情況下,你好是提醒 – 2012-03-16 02:37:07