Ajax全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。Ajax技术是目前在浏览器中通过JavaScript脚本可以使用的所有技术的集合
Ajax全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。Ajax技术是目前在浏览器中通过JavaScript脚本可以使用的所有技术的集合。Ajax以一种崭新的方式来使用所有的这些技术,使得古老的B/S方式的Web开发焕发了新的活力。
ajax()方法是jQuery底层的ajax实现,通过HTTP请求加载远程数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $.ajax({
type: "GET" ,
url: "handleAjaxRequest.action" ,
data: {paramKey:paramValue},
async: true ,
dataType: "json" ,
success: function (returnedData) {
alert(returnedData);
},
error: function (e) {
alert(e);
}
});
}
|
参数说明:
type:请求方式,“POST”或者“GET”,默认为“GET”。
url:发送请求的地址。
data:要向服务器传递的数据,已key:value的形式书写(id:1)。GET请求会附加到url后面。
async:默认true,为异步请求,设置为false,则为同步请求。
dataType:预期服务器返回的数据类型,可以不指定。有xml、html、text等。
在开发中,使用以上参数已可以满足基本需求。
如果需要向服务器传递中文参数,可将参数写在url后面,用encodeURI编码就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var chinese = "中文" ;
var urlTemp = "handleAjaxRequest.action?chinese=" +chinese;
var url = encodeURI(urlTemp);
$.ajax({
type: "GET" ,
url: url,
success: function (returnedData) {
alert(returnedData);
},
error: function (e) {
alert(e);
}
});
}
|
struts2的action对请求进行处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public void handleAjaxRequest() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType( "text/html;charset=utf-" );
response.setHeader( "pragma" , "no-cache" );
response.setHeader( "cache-control" , "no-cache" );
PrintWriter out = null ;
try {
String chinese = request.getParameter( "chinese" );
chinese = new String(chinese.getBytes( "ISO--" ), "utf-" );
System.out.println( "chinese is : " +chinese);
String resultData = "hello world" ;
out = response.getWriter();
out.write(resultData);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null ) {
out.close();
}
}
}
|
struts.xml配置文件:不需要写返回类型
1 2 3 | <action name= "handleAjaxRequest" class= "com.test.TestAction"
method= "handleAjaxRequest" >
</action>
|
分享AJAX前后台交互方法
注:ajax通过async参数决定是异步还是同步,false同步,true异步;
异步执行顺序是先执行后续动作,再执行success里代码;
同步是先执行success里代码,再执行后续代码;
验证:同步时数据量大是否会卡顿?例如从后台搜索大量数据时,页面是否卡死?
1、(异步)方法调用,后续代码不需要等待它的执行结果
后台<C#>:
1 2 3 4 5 | using System.Web.Script.Services;
public static string GetStr(string str1, string str2)
{
return str1 + str2;
}
|
前台<JQuery>:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function Test(strMsg1,strMsg2)
{
$.ajax({
type: "Post" ,
url: "Demo.aspx/GetStr" ,
async: true ,
data: "{'str1':'" +strMsg1+ "','str2':'" +strMsg2+ "'}" ,
contentType: "application/json; charset=utf-8" ,
dataType: "json" ,
success: function (data) {
alert(data.d);
},
error: function (err) {
alert(err);
}
});
$( "#pageloading" ).hide();
}
|
2、(同步)方法调用,可用于需要得到返回值是执行后续代码的前提
后台<C#>:
1 2 3 4 5 | using System.Web.Script.Services;
public static string GetStr(string str1, string str2)
{
return str1 + str2;
}
|
前台<JQuery>:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function Test(strMsg1,strMsg2)
{
var str = “”;
$.ajax({
type: "Post" ,
url: "Demo.aspx/GetStr" ,
async: false ,
data: "{'str1':'" +strMsg1+ "','str2':'" +strMsg2+ "'}" ,
contentType: "application/json; charset=utf-8" ,
dataType: "json" ,
success: function (data) {
str = data.d;
},
error: function (err) {
alert(err);
}
});
return str;
|