prototype.js 和 jQuery.js中 ajax 的使用

这次还是prototype.js 和 jQuery.js冲突的问题,前面说到过解决办法http://www.cnblogs.com/Joanna-Yan/p/4836252.html,以及上网说的大部分方法都只能解决$的冲突问题。我先引入了prototype.js 再引入jQuery.js,在用了jquery中的$,然后来用prototype中的ajax的时候,ajax的方法就不启作用了。 最后改为了用 jQuery.js中 ajax 。现在记录下两者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.8.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>
上一篇:【前端性能】必须要掌握的原生JS实现JQuery


下一篇:[系统集成] 基于telegraf, influxdb, grafana 建立 esxi 监控