• php接收post过来的 json数据 例子


    html代码

    <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的代码要加一个头具体看代码

    html代码

    <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://211.83.247.14/TempServer/jsonTest.php",//注意!这个是跟上面不一样的地方
                data : {
                    "student" : stu
                },//数据,这里使用的是Json格式进行传输
                success : function(result) {//返回数据根据结果进行相应的处理
                    alert(result);
                }
            });
        }
    </script>
    </html>

    php代码

    <?php
        header('Access-Control-Allow-Origin:*');//注意!跨域要加这个头 上面那个没有
        $student = $_POST['student'];
        echo $student['name'];
        echo $student['age'];
        echo $student['sex'];
    ?>

    这样html那边访问后就会aler出echo的信息

  • 相关阅读:
    mytest3.py-api接入平台获取数据
    对比python的进程和线程:多线程是假的
    bug-sqlite3
    安装python3 centos
    句式英语
    java组件学习15天
    areas表-省市区
    中转复制
    追踪路由的2种方法
    干掉头疼的finished with non-zero exit value 2
  • 原文地址:https://www.cnblogs.com/wmxl/p/6096876.html
Copyright © 2020-2023  润新知