1. 程式人生 > >jquery中的ajax中的done方法

jquery中的ajax中的done方法

1 jquery中1.5版本之後可以使用ajax中的done方法。該方法和ajax中的success有類似的功效。首先必須是jquery中的1.5版本之後。
如:<link rel="stylesheet" type="text/css" href="./common/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="./common/easyui/themes/default/easyui.css">

<script type="text/javascript"  src="./js/jquery-1.7.min.js"></script><!--1.5版本之後-->
<script type="text/javascript" src="./common/easyui/jquery.easyui.min.js"></script>


2 ajax的done。引數可以寫成一個方法。是將函式本身作為done的引數
如:function submitAjax(url,func){

alert("url:"+url);
jQuery.ajax({
url:url,
type:"post",
dataType:"json",
success:function(msg){

}
}).done(function(msg){
alert("執行的是done方法");
eval(func+"("+msg+")");//執行回撥函式。msg是後臺傳遞回來的引數
});
}


3 然後在js中呼叫即可。url為路徑。func為回撥函式。
如:submitAjax(urlHelper("DemoService","getDemosByQuery",obj),"showMessage");


function showMessage(msg){
  alert("這裡是回撥函式:"+msg);
}
這裡的msg為後臺傳遞回來的引數。通過resp.getWriter().print(flag);傳遞回來的flag的值。是一個字串。