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。