prototype之ajax

1 prototype.js中封装了ajax的技术。在引用了prototype.js后。如果想要ajax技术。只需要直接使用new Ajax.Request(url,{method:'',parameters:参数,onComplete: 函数 })
如:var url="./main!list";
  var param="name=xcl&password=123";
  var myAjax=new Ajax.Request(
  url,{method:'post',parameters:param,onComplete:success}
  );
解析:
url为路径。需要访问的路径地址。
method为所用的方法。为post或get。
parameters为url后所需的参数。
onComplete为当完成返回值时的回调函数,该函数的形参为XMLHttpRequest。


2 如果通过prototype.js中的ajax技术,当成功返回时会调用回调函数。改回调函数的形参为XMLHttpRequest。如果想要得到返回值,则可以通过responseText来得到返回值。
如:function success(result_s){
  var result_=eval("("+result_s.responseText+")");
  alert(result_.name_result);
  document.getElementById("result_value").value=result_.password_result;
  }


3 后台的函数,类似于正常的ajax调用后台的servlet一样,返回的值通过response.getWriter().print()来返回即可
如:public String list(){
HttpServletRequest request=MConstants.getRequest();
HttpServletResponse response=MConstants.getResponse();
String name_value=request.getParameter("name");
String password_value=request.getParameter("password");
System.out.println("得到前台的数据name: "+name_value+",password:"+password_value);
JSONObject json=new JSONObject();
json.element("name_result",name_value);
json.element("password_result",password_value);
try {
response.getWriter().print(json.toString());
} catch (IOException e) {
e.printStackTrace();

}
return null;
}


4 配置文件中不需要配置result标签,只需要配置main的路径即可。因为ajax返回的还是当前界面,而且后台函数返回的是null。
如:
<action name="main" class="userAction" >
</action>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值