2012-11-08 3 views
0

나는 뒤죽박죽이다. 내 JSON 객체가 콜백 함수로 구문 분석되지 않는 이유를 모르겠습니다. (내가 <는 len; 난 + + var에 나는 = 0, 렌 = data.features.length) {json 객체를 Google지도로 전송 vai ajax response from php script

당신의 도움 데이터 를 정의되지 않는다 :

이 형식 오류가

: 나는 콘솔 로그에 다음과 같은 오류를 얻고있다 대단히 감사합니다!

아약스 코드 :

$.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 (데이터)이 아닌 이유는 무엇입니까? –

+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

때 콘솔에서 볼 수 있습니다. 고맙습니다. –