2015-04-01 32 views
0

我有一個地圖,有標記我嘗試當用戶點擊標記在php文件中發出AJAX請求時,AJAX發送爲json標記的經度/長度和緯度/長度用戶在php文件中的文件在db中進行查詢並返回一個帶有結果幾何的json數組。當我運行mozila控制檯沒有顯示任何錯誤,如果我把ajax的alert(ar.latStart); frond顯示數據。 JSON數據點擊谷歌地圖和AJAX中的事件

var coords = {'latStart': latPos , 'longStart' : longPos , 'latend' : latitude , 'longEnd' : longitude}; 

谷歌地圖的情況下是

google.maps.event.addListener(marker, 'click', function() { 
    var latitude = this.position.lat(); 
    var longitude = this.position.lng();    
    var coords = {'latStart': latPos , 'longStart' : longPos , 'latend' : latitude , 'longEnd' : longitude};    
    getCoords(coords); 
}); 

的AJAX功能getCoords

function getCoords(ar) { 
     alert(ar.latStart); //the alert show the values is hasnt have problem 

     $.ajax({ 
       url: 'routing.php', 
       type: 'GET', 
       data: ar , 
       cache: false, 
       dataType : 'json', 
       success: function(rsp){ 
        alert(JSON.stringify(rsp)); //show an empty alert 
       } 
     }); 
} 

和PHP文件

if(isset($_GET['data'])){ 
    $obj = json_decode($_GET['data']); 
    //some php operation 
} 
echo $obj; 

Mozilla的作爲我說在CONSOL中沒有顯示任何問題顯示Ajax成功的空警報。 如有任何一個可以幫助我,我試試天,但我不能找到什麼是錯

+0

'echo json_encode($ _ GET ['data']);' – Ghostman 2015-04-01 11:11:29

+0

它並不需要php文件數組 – victor 2015-04-01 12:06:01

回答

0

,因爲在一個名爲data請求沒有鑰匙所以這是行不通的:

$_GET['data'] // because this key is not in the request. 

,而不是你可以嘗試把它想:

getCoords({"data":coords}); 

,並在php結束:

if(isset($_GET['data'])){ 
    $obj = json_decode($_GET['data']); 
//some php operation 
} 
echo json_encode($obj); /*<---encode it and echo.*/ 
+1

@soul thanks mate。 – Jai 2015-04-01 11:17:07

+0

我做了改變,它不採取PHP的JSON數組,我不明白爲什麼使它 – victor 2015-04-01 12:06:59