2012-12-08 96 views
0

這是Ajax調用看起來像json對象中的數據如何在ajax文章中傳遞?

$.ajax({ 
     url: 'http://localhost/placeadd.php', 
     dataType: "json", 
     type: "POST", 
    data: { 
     "location": { 
     "lat": -33.8669710, 
     "lng": 151.1958750 
     }, 
     "accuracy": 50, 
     "name": "Daves Test!", 
     "types": ["shoe_store"], 
     "language": "en-AU" 
     }, 
     success: function(data){ 
    console.log(data.status+"<BR>"); 
     }, 
     error: function(request, status, error){ 
     console.log(status+"<BR>"); 
     } 
     }) 
     }) 

什麼我會如何訪問每個元素在PHP文件?例如要訪問「lat」,它只是$ _POST。「location」。「lat」?

+0

'$ _ POST [ '位置'] [ '緯度']'。 –

回答

0
$.ajax({ 
    url: 'http://localhost/placeadd.php', 
    dataType: "json", 
    type: "POST", 
    data: { 
     "location": {"lat": -33.8669710,"lng": 151.1958750}, 
     "accuracy": 50, 
     "name": "Daves Test!", 
     "types": ["shoe_store"], 
     "language": "en-AU" 
    }, 
    success: function(data){ 
     console.log(data.status+"<BR>"); 
    }, 
    error: function(request, status, error){ 
     console.log(status+"<BR>"); 
    } 
}); 

在PHP:

<?php 
    $post = json_decode($_POST); 
    $lat = $post['location']['lat']; 
    echo $lat; 
?>