如何使用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);
}
});
});
答案 0 :(得分:-1)
从上面的代码result
将是一个具有天气,风和湿度指数的对象。
所以:
result.weather
将是服务器上设置的$ DCfahrenheit。
但是,consol.log(result)
的拼写错误会引发错误,并且不会处理其他代码。
答案 1 :(得分:-1)
如果要在一个对象中返回多个值,那么最好将每个返回的数据专门放入成功函数中的特定html元素中。