当前位置: 移动技术网 > IT编程>脚本编程>Ajax > Ajax()方法如何与后台交互

Ajax()方法如何与后台交互

2017年12月08日  | 移动技术网IT编程  | 我要评论
ajax全称为“asynchronous javascript and xml”(异步javascript和xml),是指一种创建交互式网页应用的网页开发技术。ajax技术

ajax全称为“asynchronous javascript and xml”(异步javascript和xml),是指一种创建交互式网页应用的网页开发技术。ajax技术是目前在浏览器中通过javascript脚本可以使用的所有技术的集合。ajax以一种崭新的方式来使用所有的这些技术,使得古老的b/s方式的web开发焕发了新的活力。

ajax()方法是jquery底层的ajax实现,通过http请求加载远程数据。

$.ajax({
type: "get",
url: "handleajaxrequest.action",
data: {paramkey:paramvalue},
async: true,
datatype:"json",
success: function(returneddata) {
alert(returneddata);
//请求成功后的回调函数
//returneddata--由服务器返回,并根据 datatype 参数进行处理后的数据;
//根据返回的数据进行业务处理
},
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编码就可以了。

var chinese = "中文";
var urltemp = "handleajaxrequest.action?chinese="+chinese;
var url = encodeuri(urltemp);//进行编码
$.ajax({
type: "get",
url: url,//直接写编码后的url
success: function(returneddata) {
alert(returneddata);
//请求成功后的回调函数
//returneddata--由服务器返回,并根据 datatype 参数进行处理后的数据;
//根据返回的数据进行业务处理
},
error: function(e) {
alert(e);
//请求失败时调用此函数
}
});
} 

  struts2的action对请求进行处理:

public void handleajaxrequest() {
httpservletrequest request = servletactioncontext.getrequest();
httpservletresponse response = servletactioncontext.getresponse();
//设置返回数据为html文本格式
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);
//如果返回json数据,response.setcontenttype("application/json;charset=utf-");
//gson gson = new gson();
//string result = gson.tojson(resultdata);//用gson将数据转换为json格式
//out.write(result);
out.flush();
}catch(exception e) {
e.printstacktrace();
}finally {
if(out != null) {
out.close();
}
}
}

  struts.xml配置文件:不需要写返回类型

<action name="handleajaxrequest" class="com.test.testaction"
method="handleajaxrequest">
</action>

分享ajax前后台交互方法

注:ajax通过async参数决定是异步还是同步,false同步,true异步;

  异步执行顺序是先执行后续动作,再执行success里代码;

  同步是先执行success里代码,再执行后续代码;

验证:同步时数据量大是否会卡顿?例如从后台搜索大量数据时,页面是否卡死?

1、(异步)方法调用,后续代码不需要等待它的执行结果

  后台<c#>:

using system.web.script.services; 
public static string getstr(string str1, string str2) 
{ 
return str1 + str2; 
}

前台<jquery>:

function test(strmsg1,strmsg2) 
{
$.ajax({
type: "post",
url: "demo.aspx/getstr",
async: true,
//方法传参的写法一定要对,与后台一致,区分大小写,不能为数组等,str1为形参的名字,str2为第二个形参的名字 
data: "{'str1':'"+strmsg1+"','str2':'"+strmsg2+"'}",
contenttype: "application/json; charset=utf-8",
datatype: "json",
success: function(data) {
//返回的数据用data.d获取内容 
alert(data.d);
},
error: function(err) {
alert(err);
}
});
  //隐藏加载动画
$("#pageloading").hide();
}

2、(同步)方法调用,可用于需要得到返回值是执行后续代码的前提

  后台<c#>:

using system.web.script.services; 
public static string getstr(string str1, string str2) 
{ 
return str1 + str2; 
}


前台<jquery>:

function test(strmsg1,strmsg2) 
{
 var str = “”;
$.ajax({
type: "post",
url: "demo.aspx/getstr",
async: false,
//方法传参的写法一定要对,与后台一致,区分大小写,不能为数组等,str1为形参的名字,str2为第二个形参的名字 
data: "{'str1':'"+strmsg1+"','str2':'"+strmsg2+"'}",
contenttype: "application/json; charset=utf-8",
datatype: "json",
success: function(data) {
//返回的数据用data.d获取内容 
str = data.d;
},
error: function(err) {
alert(err);
}
});
 return str;

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网