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