如何使用json将json发送到jquery的php数组?

时间:2014-12-28 00:19:46

标签: php jquery ajax json

我正在构建一个计算器并通过在jquery中使用ajax来获取结果,以便将值发送到php文件并将结果返回给jquery。我可以得到一个结果返回,但想得到两个可以分开的结果。我尝试使用json from a previous answer执行此操作,但无法使其正常工作。似乎答案来自JQuery。我要离开的是什么?

JQuery的

$(document).ready(function () {
$("#loanamount").change(function() {
    var amount = $('#loanamount').val();
    var rate = $('#rate').val();
    var term = $('#term').val();
    $.post('superGlobalsGet.php', {amount: amount, rate: rate, term: term}, function(data) {
        $('#payment').html(data.payment); 
        $('#totalint').html(data.totalint);
    }, "json");
});

PHP

<?php

    if(isset($_POST['amount'], $_POST['rate'], $_POST['term'])) {
        $amount = $_POST['amount'];
        $rate = $_POST['rate'];
        $term = $_POST['term'];
        $convertRate = $rate + 2;
        $newRate = $convertRate + 3;
        $convertTerm = $term + 4;
        $loanPay = $newRate + 5;
        $totInt = ($loanPay * $convertTerm) - $amount;

        echo json_encode(array("payment" => $loanPay, "totalint" => $totInt));
    }
?>

1 个答案:

答案 0 :(得分:0)

在发送回复之前添加内容类型:

...
header('Content-Type: application/json');
echo json_encode(array("payment" => $loanPay, "totalint" => $totInt));