ajax是目前js中web前臺請求後臺數據的主流方法,下面簡單介紹一下。
在jQuery中使用ajax,首先在function一個方法,在方法中採用$.ajax({});使用ajax請求數據,如:
function isHigherVersion(){
var newversion;
//本地控件當前版本
var version = WebClient.GetProductVersion();
//獲取properties中服務端上傳的版本
$.ajax({
type: "POST",
url: "ManageAjax!getVersionInfo",
data: "user.userToken="+ userToken,
async: false,
dataType: 'json',
success:function(data){
version = version.split("."); if(data[0].versionInfo0>version[0]||data[1].versionInfo1>version[1]||data[2].versionInfo2>version[2]||data[3].versionInfo3>version[3]){
isHigher = true;
}
newversion = data[0].versionInfo0+"."+data[1].versionInfo1+"."+data[2].versionInfo2+"."+data[3].versionInfo3;
},
error:function(data){
ShowError("數據請求失敗!");
}
});
if(isHigher){
versionupdate(newversion);
}
}
主要的幾個參數:type:請求類型post/get,url:請求資源的URL,data:提交給服務器(後臺)的數據,dataType: 數據類型text/json/jsonp/html/xml,async: 同步/異步請求(true/false),默認爲true異步,success:function(data){請求成功後執行},error:function(data){請求失敗後執行}
若後臺採用Struts2框架,URL指定action。