zoukankan      html  css  js  c++  java
  • ajax常用操作

    load的方法的使用(现在已不常用)

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>demo</title>

    <!-- 引进jQuery -->

    <script src="jquery.js"></script>
    <style type="text/css">
      input{
        margin-top: 30px;
      }
      div{
        margin-top: 20px;
         150px;
        height: 60px;
        border:2px solid red;
      }
    </style>


    </head>
    <body>

    <input type="button" value="button-1" id="button1" />
    <div id="content1"></div>

    <input type="button" value="button-2" id="button2" />
    <div id="content2"></div>

    <input type="button" value="button-3" id="button3" />
    <h2 style="display:none" id="img">加载中...</h2>
    <div id="content3"></div>

    <script type="text/javascript">
      $('#button1').click(function () {
      //Math.random()能够解决严重的缓存问题,特别对于ie
        $('#content1').load('demo1.php?'+Math.random(),function(msg){
          $('#content1').html(msg);
        })
      });

      $('#button2').click(function () {
      //在html中获取地址栏中传递的参数
        $('#content2').load('demo2.html#one?'+Math.random(),function(msg){
          $('#content2').html(msg);
        })
      });

      $('#button3').click(function () {
      //在html中获取地址栏中传递的参数
        $('#content3').load('demo3.php?'+Math.random(),function(msg){
          $('#content3').html(msg);
        })
      });

    </script>
    </body>
    </html>

    demo1.php的内容是:

      <?php
        echo '这是php文件返回的内容,将会返回到div里';

      ?>

    demo2.html的内容是:

    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>demo2</title>
      </head>
      <body>
        <span id="one" style="color:red">我是id为one里span里的内容</span>
        <span id="two" style="color:blue">我是id为two里span里的内容</span>
      </body>
    </html>

    demo3.php的内容是:

      <?php
         sleep(3);//3秒后响应

         echo '111';

      ?>

    ajax----表单序列化

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>demo</title>

    <!-- 引进jQuery -->

    <script src="jquery.js"></script>

    </head>
    <body>

      <form method="post">
        a项:<input type="text" name="a" /><br/>
        b项:<input type="text" name="b" /><br/>
        c项:<input type="text" name="c" /><br/>
        d项:<input type="text" name="d" /><br/>
        e项:<input type="text" name="e" /><br/>
        f项:<input type="text" name="f" /><br/>
        <input type="button" id="submit" value="提交" />
      </form>

      <script type="text/javascript">

      //表单序列化,一定要包含在form里,每个元素要有name属性
        $('#submit').click(function () {
        //表单序列化得到所有数据
        var data = $('form').serialize();
        $.ajax({
          type: "POST",
          url:'demo4.php',
          data:data,// 要提交的表单
          success: function(ms) {
          alert($('form').serialize());
          alert(ms);
          },
        error:function(jqXHR,textStatus,errorThrown){
          if(errorThrown == 'Not Found'){
            console.log('请求地址不存在');
          }
          if(textStatus == 'timeout'){
            console.log('请求超时');
          }
        }
      });
    })

    </script>
    </body>
    </html>

    demo4.php的内容是:

      <?php
        echo $_POST['a'];

      ?>

    ajax操作xml,json

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>demo</title>

    <!-- 引进jQuery -->

    <script src="jquery.js"></script>

    </head>
    <body>

       <input type="button" value="button-5" id="button5" />

       <input type="button" value="button-6" id="button6" />

         <script type="text/javascript">

      //ajax操作xml

        $('#button5').click(function(){
          $.ajax({
            url:'stu.xml?'+Math.random(),
            type:'get',
            dataType:'xml',
            success:function(xml){
              $(xml).find('title').each(function(){
                //操作xml文件是,html()方法不可用
                alert($(this).children("li").text())
              });
            }
          })
        })

      //ajax操作json,eval的妙用
        $('#button6').click(function(){
          $.ajax({
            url:'demo6.php?'+Math.random(),
            type:'get',
            dataType:'json',
            success:function(rs){
              alert(eval(rs));//object
              alert(eval(rs[0].AreaId));//123
            }
          })
        })

         </script>
    </body>
    </html>

    stu.xml的内容是:

      <?xml version="1.0" encoding="UTF-8"?>

      <stu>
        <title>
          <li>aa</li>
        </title>
        <title>
          <li>bb</li>
        </title>
        <title>
          <li>cc</li>
        </title>
      </stu>

    demo6.php的内容是:

      <?php
       

        $strJSON = '[{"AreaId":"123"},{"AreaId":"345"}]';

        echo $strJSON;

      ?>

  • 相关阅读:
    HBase-scan API 通过scan读取表中数据
    Servlet总结
    【吴节操点评】中国企业SaaS应用深谙未来者寥寥数几 两极分化将加剧
    hibernate实战笔记1---初探
    POJ
    系统服务-----Activity服务的获取getSystemService
    我的编程之路(二十) 新的环境、新的開始
    王爽《汇编语言》第2版-----2、寄存器
    更新智能开发研发进度
    libcurl实现解析(3)
  • 原文地址:https://www.cnblogs.com/liuwanqiu/p/5763044.html
Copyright © 2011-2022 走看看