ajax常用操作

來源:互聯網
上載者:User

標籤:

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;
    width: 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;

  ?>

 

ajax常用操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.