一、可以设置全局属性,这样以后调用起来很简洁。
jQuery.ajaxSetup() 代码:
$.ajaxSetup({ url: "/xmlhttp/", global: false, type: "POST"}); $.ajax({ data: myData });
二、jquery ajax全解析
03、jquery ajax全解析--jQuery.getScript( url, [callback] ) : 通过 GET 方式请求载入并执行一个 JavaScript 文件。
使用jQuery加载js脚本
案例:使用财付通加载银行列表时,就采用的getScript,由于加载时间太长,所以在加载过程中放入了一个div动画,等待加载完毕隐藏此div。
<!--财付通B2C提供的银行列表样式,共有三种,这是最窄的一种--> <link rel="stylesheet" type="text/css" href="http://union.tenpay.com/bankList/css_col2.css"/> <!--jquery js库--> <script type='text/javascript' src='http://union.tenpay.com/bankList/jquery.js'></script> <!--加载银行列表,并放置到div--> <script>$.getScript("http://union.tenpay.com/bankList/bank.js");</script> <!--放置银行列表的div块--> <div id="tenpayBankList" style="text-align: center;"></div> <!--用户选择的银行类型值,默认财付通--> <input type="hidden" name="bank_type_value" id="bank_type_value" value="0"> ////////////////////////////////////////////////////////////////////////////////// <script type="text/javascript"> //getScript加载完毕后,关闭转圈图 $.ajaxSetup({ cache: true, beforeSend:function(XMLHttpRequest){ console.log("getScript 开始加载银行列表..."); $("#loading").show(); }, //success请求成功之后 success:function(result){ console.log("getScript 成功..."); $("#loading").hide(); }, //complete无论成功与否,都会执行 complete:function(XMLHttpRequest,textStatus){ console.log("getScript 完成..."); $("#loading").hide(); }, error:function(XMLHttpRequest){ console.log("getScript 加载失败..."); } }); </script> function payFrm(){ //后续添加的,用于判断用户是否选择了一个银行 if(!$('input:radio[name="bank_type"]').is(":checked")){ alert("提醒:请选择银行!"); return false; } }