如何在ajax帖子中传递json对象中的数据?

时间:2012-12-08 06:34:20

标签: php jquery 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”?

1 个答案:

答案 0 :(得分: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;
?>