当前位置: 代码迷 >> JavaScript >> prototype.js 跟 jQuery.js ajax 调用
  详细解决方案

prototype.js 跟 jQuery.js ajax 调用

热度:566   发布时间:2013-04-02 12:35:26.0
prototype.js 和 jQuery.js ajax 调用

项目模块扩展团队不同,采用框架不同,前台有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>



 

1楼lfmilaoshi昨天 23:12
你的博客,相对一年前,有进步了?
  相关解决方案