欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

prototype.js簡(jiǎn)單實(shí)現(xiàn)ajax功能示例

 更新時(shí)間:2017年10月18日 10:28:45   作者:sliufen  
這篇文章主要介紹了prototype.js簡(jiǎn)單實(shí)現(xiàn)ajax功能,結(jié)合實(shí)例形式分析了prototype.js前臺(tái)實(shí)現(xiàn)ajax與后臺(tái)struts的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了prototype.js簡(jiǎn)單實(shí)現(xiàn)ajax功能。分享給大家供大家參考,具體如下:

原本不知道prototype.js是一個(gè)框架,只當(dāng)其是一個(gè)再普通不過(guò)的JS文件.隨手拿著用了用,寫了一個(gè)JSP頁(yè)面,單純的用prototype.js來(lái)實(shí)現(xiàn)AJAX效果.用了之后發(fā)現(xiàn)超好用,自己再也不用寫那么大一堆代碼了,哦耶.言歸正傳,還是把今天寫的那個(gè)小代碼發(fā)上來(lái).

一.JSP部分

這部分的代碼,最為關(guān)鍵的是JS部分的改變.沒(méi)有采用prototype.js的時(shí)候,生成一個(gè)AJAX效果,起碼得有四大段.現(xiàn)在,只用寫成下面這一小段代碼了.

<script type="text/javascript">
function getnodelist(){
  function onSuccess(request)
  {
      alert("success");
     $("result").innerHTML = "abc"+request.responseText ;
  }
  function onComplete(request){
  }
  function onFailure(request){
     alert("failure");
     $("result").innerHTML = request.responseText ;
  }
  var paras = "" ;
  var ajax = new Ajax.Request(
     "http://localhost:8080/LoginDemo/test.do",
    {
    method: 'post',
    parameters:paras ,
    onSuccess: onSuccess,
    onComplete:onComplete,
    onFailure:onFailure
   }
 );
}
</script>

其中最重要的就是這一段了:

var ajax = new Ajax.Request(   //新生成一個(gè)AJAX.Request對(duì)象.
 "http://localhost:8080/LoginDemo/test.do", //請(qǐng)求的servlet地址.即URL
 {                                //參數(shù)
  method: 'post',
  parameters:paras ,
  onSuccess: onSuccess,       //這些函數(shù)和上面三個(gè)函數(shù)相對(duì)應(yīng).
  onComplete:onComplete,
  onFailure:onFailure
 });

注明:,里面的URL要么寫成絕對(duì)路徑,要么就在前面取<% String path = request.getContextPath();%>,然后在這里
"<%=path%>/test.do"

prototype.js讓我覺(jué)得最方便的地方就在于我不用自己去判斷當(dāng)前瀏覽器的狀態(tài),如果成功了就調(diào)用OnSuccess函數(shù),失敗就調(diào)用onFailure函數(shù),而我只用關(guān)注于成功失敗之后該怎么處理,簡(jiǎn)化了程序.

二.后臺(tái)struts部分

public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) {
    // TODO Auto-generated method stub
    try{
      System.out.println("in action");
     response.setContentType("text/html;charset=gb2312");
      ServletOutputStream out = response.getOutputStream();
      out.print("hello slf!");
      System.out.println("out");
    }catch(Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }

簡(jiǎn)單的打印.

希望本文所述對(duì)大家prototype.js框架的程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論