prototype.js 和 jQuery.js ajax 調用

來源:互聯網
上載者:User

項目模組擴充團隊不同,採用架構不同,前台有prototype轉為jQuery,下示記錄ajax調用區分

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><script type="text/javascript" src="./js/prototype/prototype.js"></script><script type="text/javascript" src="./js/jquery/jquery-1.6.2.min.js"></script><script type="text/javascript" language="javaScript">function callServletByPrototypeAjax(){new Ajax.Request("test1.prototype",{parameters: "para1=val1¶2=val2",method:'post',onComplete:function(transport){alert("er");var valReturned = transport.responseText;alert("value returned from server port is " + valReturned);}});}function callServletByJQueryAjax(){jQuery.ajax({type:"post",url:"test1.jQuery?para1=value1",dataType:"text",success:function(data){alert("value returned from sever is : " + data );}});}</script></head><body><!--<form action=".do" method="get">--><form name="formRequest" action=".do" method="post"><input type="text" name="form1Para1" value="value1"/><input type="submit"  value="submit to show page"/></form><form name="prototypeAjaxRequest"><input type="button" value="call servlet by prototype Ajax" onclick="callServletByPrototypeAjax();"/></form><form name="JQueryAjax"><input type="button" id="callJQueryAjax" value="call servlet by JQuery Ajax" onclick="callServletByJQueryAjax();"/></form></body></html>

 

當然,在使用jQuery時,可以使用如下方式給組件註冊相關事件。需要注意prototype和jQuery對$符的衝突。

$(document).ready(function(){$("#callJQueryAjax").click(function(){jQuery.ajax({type:"post",url:"test1.jQuery?para1=value1",dataType:"text",success:function(data){alert("value returned from sever is : " + data );}});});};

上述Ajax申請資源為普通servlet,web.xml簡要配置如下,亦可在servlet3.0下使用註解方式。

<servlet>    <servlet-name>test1</servlet-name>    <servlet-class>com.test.Test1OfPrototype</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>test1</servlet-name>    <url-pattern>/test1.prototype</url-pattern>  </servlet-mapping>  <servlet>    <servlet-name>test1JQuery</servlet-name>    <servlet-class>com.test.Test1OfJQuery</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>test1JQuery</servlet-name>    <url-pattern>/test1.jQuery</url-pattern>  </servlet-mapping>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.