zoukankan      html  css  js  c++  java
  • Ajax——jq中ajax的使用

    格式化表单

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="js/jquery.min.js"></script>
    </head>
    <body>
    <form action="11.php" method="get" id="form">
        <input type="text" name="text" value="wq"><br>
        <input type="text" name="pwd" value="123"><br>
        <input type="text" name="sex" value="nv">
    </form>
    <script>
        $(function () {
            var a = $('#form').serialize();
            console.log(a);//text=wq&pwd=123&sex=nv
            $.get('11.php', a, function (data) {
                console.log(data);//wq
            });
        });
    </script>
    </body>
    </html>
    <?php
      header("content-type:text/html;charset=utf-8");
      echo $_GET['text'];
    ?>

    异步提交

    1、参数的顺序要正确:url、data、success、dataType

    2、最后一个dataType可以不写,如果写了json,那么返回的数据会自动进行js对象的转化JSON_parse()

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="js/jquery.min.js"></script>
    </head>
    <body>
    <button>get异步</button>
    <button>post异步</button>
    <script>
        $(function () {
            $('button:eq(0)').on('click',function () {
                $.get('12.php',{name:'wq',age:12},function (data) {
                    console.log(data);//wq==12
                })
            });
            $('button:eq(1)').on('click',function () {
                $.post('13.php',{name:'qx',age:14},function (data) {
                    console.log(data);//qx===14
                })
            });
        });
    </script>
    </body>
    </html>
    <?php
      header("content-type:text/html;charset=utf-8");
      echo $_GET['name'].'=='.$_GET['age'];
    ?>
    <?php
      header("content-type:text/html;charset=utf-8");
      echo $_POST['name'].'==='.$_POST['age'];
    ?>

    ajax综合

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="js/jquery.min.js"></script>
    </head>
    <body>
    <button>ajax异步</button>
    <script>
        $(function () {
            $('button:eq(0)').on('click',function () {
                $.ajax({
                    url:'13.php',
                    data:{name:"qx",age:17},
                    success:function (data) {
                        console.log(data);//qx===17
                    },
                    type:'post',
                    beforeSend:function () {
                        console.log('发送之前调用');
                    },
                    error:function () {
                        console.log('数据接收错误');
                    }
                });
            });
        });
    </script>
    </body>
    </html>
    <?php
      header("content-type:text/html;charset=utf-8");
      echo $_POST['name'].'==='.$_POST['age'];
    ?>
  • 相关阅读:
    部分模块
    正则表达式和re模块
    软件开发的目录规范
    模块以及模块的导入
    迭代器、可迭代对象、迭代器对象、生成器、生成器表达式和相关的面试题
    内置方法补充
    递归、二分法、匿名函数和部分内置方法
    python的闭包和装饰器
    函数名本质、函数命名空间和作用域链
    函数简介和函数的结构分析
  • 原文地址:https://www.cnblogs.com/wuqiuxue/p/8182724.html
Copyright © 2011-2022 走看看