百度上說async:false,就是同步了。測試後發現不是javascript
<script type="text/javascript"> function callAjax() { var value = '1'; jQuery.ajax({ async:false, url:"Handler1.ashx", //type:"post", success:function(msg){ value = msg; } }); return value; } $(function myfunction() { alert(callAjax()); }); </script>
結果仍是返回 1.java
關鍵方法來了:
jQuery.ajax({
async:false,
url:"Handler1.ashx",
//type:"post",
success:function(msg){
value = msg;
}
}).done(function(msg){
value = msg;
});ajax
加個done纔是真正的同步。返回後臺數據async