zoukankan      html  css  js  c++  java
  • php接收post过来的json数据

    <html>
    <head>
        <title>json</title>
        <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.js"></script>
    </head>
    <body>
        json
        <input type="button" onclick="sendJson()" value="点击">
    </body>
    
    <script>
        function sendJson() {
    
            var stu={
                name:"冷荣富",
                age:22,
                sex:"男"
            };
            $.ajax({
                type : "POST",  //提交方式
                url : "http://localhost/jsonTest.php",//路径,www根目录下
                data : {
                    "student" : stu
                },//数据,这里使用的是Json格式进行传输
                success : function(result) {//返回数据根据结果进行相应的处理
                    alert(result);
                }
            });
        }
    </script>
    </html>
    

    php代码

    <?php
        $student = $_POST['student'];
        echo $student['name'];
        echo $student['age'];
        echo $student['sex'];
    ?>

    这是在一台电脑上的,如果两台电脑就设计到跨域的问题,html的代码要把url改一下,php的代码要加一个头具体看代码

    header('Access-Control-Allow-Origin:*');//注意!跨域要加这个头 上面那个没有
    每天进步一点点~~~
  • 相关阅读:
    第十周进度条
    冲刺阶段第十天
    冲刺阶段第九天
    冲刺阶段第八天
    冲刺阶段第七天
    冲刺阶段第六天
    第一次冲刺阶段(十一)
    第一次冲刺阶段(十)
    第一次冲刺阶段(九)
    第一次冲刺阶段(八)
  • 原文地址:https://www.cnblogs.com/heshimei/p/8459121.html
Copyright © 2011-2022 走看看