夺命雷公狗jquery---54通过ajax的底层实现返回json格式的数据

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="js/jquery.js"></script>
        <script>
            $(function(){
                $('#btnok').bind('click',function(){
                    $.ajax({
                        type:'post',
                        url:'ajax3.php',
                        data:'first=100&second=20',
                        dataType:'json',
                        success:function(msg){
                            alert(msg.jia+'--'+msg.jian+'--'+msg.cheng+'--'+msg.chu);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <input type="button" id="btnok" value="OK" />
    </body>
</html>

PHP的代码:

<?php
    $first = $_POST['first'];
    $second = $_POST['second'];

    $jia = $first+$second;
    $jian = $first-$second;
    $cheng = $first*$second;
    $chu = $first/$second;

    $arr = array(
        'jia'=>$jia,
        'jian'=>$jian,
        'cheng'=>$cheng,
        'chu'=>$chu
    );

    echo json_encode($arr);
原文地址:https://www.cnblogs.com/leigood/p/4922307.html