2012-11-08 70 views
0

我很難過。我不確定爲什麼我的JSON對象沒有被我的回調函數解析。 我收到以下錯誤在控制檯日誌:發送json對象到谷歌地圖從php腳本的ajax響應

類型錯誤:數據不確定 爲(VAR I = 0,LEN = data.features.length;我< LEN;我++){

您的幫助非常感謝!

Ajax代碼:

$.ajax({ 
     type : 'POST', 
     url : 'timezone.php', 
     dataType : 'json', 
     data  : { "func" : "getFirstAndNext", epochTime : browserUTC }, 
     success : function(data) { 
       console.log(data.first); 
       console.log(data.next); 
       sector_callback(data.first); 

      // do something with data.myObject.memberVariable 

     }, 
     error : function (XMLHttpRequest, textStatus, errorThrown) { 
      // didn't work! 
     } 
    }); 

PHP腳本:

<?php 
    //header('Content-Type: application/json; charset=utf-8'); 
    function getFileStamp($type) { 
     $timeInterval = 15; // minutes 
     $now = round($_POST['epochTime']/1000); 
     $now = round(1352324181061 /1000); // 3:36:21 
     //$now = round(1352238011.067); 
     $offsetInSeconds = $timeInterval * 60; 
     $offsetInMillis = $timeInterval * 60 * 1000; 

     $currentFileTime = $now - $now % ($timeInterval * 60); 
     //$currentFileTime = ($now - ($now % $offsetInMillis))/1000.0; // this returns epoch time in $timeInterval minutes. 
     $nextFileTime = $currentFileTime + $offsetInSeconds; // next epoch time for file; 
     return ($type == 0) ? $currentFileTime : $nextFileTime; 
    } 

     $currentFileTime = getFileStamp(0) . '.json'; 
     $nextFileTime = getFileStamp(1) . '.json'; 

     $res = array(); 
     $res['file1'] = file_get_contents($currentFileTime); 
     $res['file2'] = file_get_contents($nextFileTime); 
     echo json_encode(array("first"=>$res['file1'],"next"=>$res['file1'])); //takes contents and converts it to json object 
    ?> 

該行業回調函數:

var allPolygons = []; 
    function sector_callback() { 
     //console.log(data); 
     var bounds = new google.maps.LatLngBounds();   
     for (var i = 0, len = data.features.length; i < len; i++) { 
      var coords = data.features[i].geometry.coordinates[0]; 
      siteNames = data.features[i].properties.Name; // added for site names 
      var path = []; 
     for (var j = 0, len2 = coords.length; j < len2; j++){ // pull out each  set of coords and create a map object 
      var pt = new google.maps.LatLng(coords[j][1], coords[j][0]) 
      bounds.extend(pt); 
      path.push(pt); 

     } 

     var polygons = new google.maps.Polygon({ 
     path: path, 
      strokeColor: "#000000", 
      strokeOpacity: 0.8, 
      strokeWeight: 1, 
      fillColor: "#000000", 
      fillOpacity: 0.35, 
     map: map 
     }); 
     createClickablePoly(polygons, siteNames); 

     google.maps.event.addListener(polygons, 'mouseover', function() { 
     var currentPolygon = this; 

     currentPolygon.setOptions({ 
      fillOpacity: 0.45, 
      fillColor: "#FF0000" 
      }) 
     }); 

     google.maps.event.addListener(polygons, 'mouseout', function() { 
     var currentPolygon = this; 
     currentPolygon.setOptions({ 
      fillOpacity: 0.35, 
      fillColor: "#000000" 
      }) 
     }); 

     allPolygons.push(polygons); 
     } 
    } 
+0

data.first定義了嗎?爲什麼它不是sector_callback(data); – 2012-11-08 17:43:24

+0

我想返回兩個JSON對象。我可以在console.log(data.first)和colsole.log(data.next) –

回答

1

都會響起只有我能看到這裏,我沒有看到你的sector_callback()如何訪問數據。當jQuery傳入數據時,如果一切順利,控制檯應該記錄這些屬性。但是當你將一些數據傳遞給你的sector_callback()時,它就會丟失。原因是您的sector_callback()需要讀取參數[]數組來獲取它,或者您需要將函數簽名更改爲sector_callback(data)。

在你的jQuery塊,傳遞數據,而不是data.features:

sector_callback(data); 

,改變你自己的回調簽名:

function sector_callback(data) { 
    console.log(data); // will log the whole chunk of the server data returned 

... 

} 

這是因爲數據是在一個封閉的時候它回來到你的jQuery回調。即使你已經把它聲明在關閉之外的某個地方,它也會保留本地作用域,除非你明確地傳遞它,否則你的其他函數將無法訪問它。

希望我已經理解了問題的癥結所在,這對我有幫助...

+0

中看到它們,這確實有道理。謝謝。 –