我已经设置了一些 ajax,我现在正在测试它,它背后的想法几乎是将一些数据从下拉框发送到 php 脚本,进行一些计算然后返回结果,它做得很好并返回结果,但现在不只是发回一个结果并输出,我想发回多个结果并输出它们,我能够将多个数据发送到 php 脚本,所以我确信我可以发回多个。
无论如何,它只返回第一个结果而不返回其余结果。
这是AJAX
<script>
$("document").ready(function (){
$(".add_extension").change(function(){
var m = document.getElementById('meter_square');
var meter_square = m.options[m.selectedIndex].value;
var s = document.getElementById('story_height');
var story_height = s.options[s.selectedIndex].value;
$.ajax({
type: "GET",
url: "script.php",
data: { meter_square: meter_square, story_height: story_height },
dataType: "json",
statusCode: {
200: function (result, result2)
{
$("#expected_gain").html(result.value);
$("#house_price").html(result2.value2);
}
}
});
})
});
</script>
这是php脚本
<?php
$meter_square = $_GET["meter_square"];
$story_height = $_GET["story_height"];
$result = $meter_square + $story_height;
$result2 = $meter_square * $story_height;
echo json_encode(array("value" => $result, "value2" => $result2));
?>
你可以看到我已经尝试过我认为可能有效的方法,如果你需要任何其他代码或希望我删除我添加的不起作用的代码,请告诉我。
感谢所有的帮助
最佳答案
您只会收到一个响应对象:
function (response) {
$("#expected_gain").html(response.value);
$("#house_price").html(response.value2);
}
关于php - 通过 AJAX 从 PHP 发回多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593772/