如何在AJAX方法中访问JSON数据?

时间:2017-11-01 14:48:23

标签: php jquery json ajax

如何使用PHP编码访问数据到$ajax()方法?

我的问题主要是用作变量的内容以及数据属性的值应该是什么?这是我到目前为止的代码。我是AJAX的新手,我很感激你的回答,谢谢。

$dcweather = array('weather' => "$DCfahrenheit", 'wind' => "$DCwind", 'humidity' => "$DChumidity");
$jsonCode = json_encode($dcweather);
echo ($jsonCode);
('#button_dc').click(function() {
  var data = {
    weather: "$DCfahrenheit",
    wind: "DCwind",
    humidity: "DChumidity"
  }

  $.ajax({
    type: 'POST',
    url: 'DCweather.php',
    data: data,
    dataType: 'json',
    success: function(result) {
      consol.log(result);
      $('#div_new').replaceWith(result);
    }
  });
});

2 个答案:

答案 0 :(得分:-1)

从上面的代码result将是一个具有天气,风和湿度指数的对象。

所以:

result.weather将是服务器上设置的$ DCfahrenheit。

但是,consol.log(result)的拼写错误会引发错误,并且不会处理其他代码。

答案 1 :(得分:-1)

如果要在一个对象中返回多个值,那么最好将每个返回的数据专门放入成功函数中的特定html元素中。