jquery的ajax中的幾種狀態

來源:互聯網
上載者:User
xml檔案
myfile.xml
<?xml version="1.0" encoding="utf-8" ?>
<List>
  <CB cname="北京" cid="1">
    <CS cname="北京" cid="364">北京市</CS>    
  </CB>
  <CB cname="天津" cid="2">
    <CS cname="天津" cid="365">天津市</CS>
  </CB>
  <CB cname="上海" cid="3">
    <CS cname="上海" cid="364">上海市</CS>
  </CB>
  <CB cname="重慶" cid="4">
    <CS cname="重慶" cid="365">重慶市</CS>
  </CB>
</List>
*********************************************************

展示頁面xml.php
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>xml運用</title>
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
$(document).ready(function(){
$("button").click(function(){
  $.ajax({
   type:"post",
   url:"myfile.xml",
   dataType:"xml",
   error:function(){
    alert("有錯誤。");
   },
   success:function(xml){
    $(xml).find("CB").each(function(){
      var cs = $(this).find("CS").text();
      alert($(this).find("CS").attr("cname"));
      $("#show").append("<font color=maroon>" + cs + "</font><hr />");
    });
   }
  });
});
$("#show").ajaxStart(function(){
  $(this).append("<font color=red>1、準備開始</font>");
});
$("#show").ajaxSend(function(){
  $(this).append("<font color=red>2、開始發送</font>");
});
$("#show").ajaxSuccess(function(){
  $(this).append("<font color=red>3、傳送成功</font>");
});
$("#show").ajaxComplete(function(){
  $(this).append("<font color=red>4、傳送完成</font>");
});
$("#show").ajaxStop(function(){
  $(this).append("<font color=red>5、傳送停止</font>");
});
$("#show").ajaxError(function(){
  $(this).append("<font color=red>傳送失敗</font>");
});

});
</script>
</head>
<body>
<button onclick="startajax();">開始ajax</button>
<div id="show"></div>
</body>
</html>
//注意:ajaxSend,ajaxStart等都是全域事件,放到其他事件外才能正確顯示,否則只能運行ajaxSuccess,ajaxComplete,ajaxStop
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.