我用myEclipse5.0可以正确运行一个AJAX简单运行的例子
但是我朋友哪是myEclipse6.0,问题就出现了
xmlHtmlRequest为空或者不是对象。请各位高人朋友帮忙看看 JSP代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Ajax example</title>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var XMLHttpReq;
function createXMLHttpRequest()
{
if(window.XMLHttpRequest){
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXOject)
{
try{
XMLHttpReq = new ActiveXObject("Msxm12.XMLHTTP");
}catch (e){
try{
XMLHttpReq = new ActiveXOject("Microsoft.XMLHTTP");
}catch(e){
}
}
}
}
//发送请求函数
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange = processResponse; //指定响应函数
XMLHttpReq.send(null);
}
//处理返回信息函数
function processResponse(){
if(XMLHttpReq.readyState == 4){
if(XMLHttpReq.status == 200){
var res = XMLHttpReq.responseXML.getElementsByTagName("res")[0].firstChild.data;
window.alert(res);
}else{
window.alert("您所请求的页面有问题。");
}
}
}
//注册函数
function signUp(){
var uname = document.myform.uname.value;
var psw = document.myform.psw.value;
var psw2 = document.myform.psw2.value;
if(uname == ""){
window.alert("用户名不能为空");
document.myform.uname.focus();
return false;
}
else if(psw != psw2){
window.alert("两次密码不正确");
document.myform.psw.focus();
return false;
}
else{
sendRequest('signUp?uname='+ uname + '&psw=' +psw);
}
}
</script>
</head>
<body vLink ="#006666" link="#003366" bgColor="#E0F0F8">
<form action="" method="post" name="myform">
用户名:<input size="15" name="uname"><p>
密码:<input type="password" size="15" name="psw"><p>
重输密码:<input type="password" size="15" name="psw2"><p>
<input type="button" value="注册" onclick="signUp()">
</form>
</body>
</html>
但是我朋友哪是myEclipse6.0,问题就出现了
xmlHtmlRequest为空或者不是对象。请各位高人朋友帮忙看看 JSP代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Ajax example</title>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var XMLHttpReq;
function createXMLHttpRequest()
{
if(window.XMLHttpRequest){
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXOject)
{
try{
XMLHttpReq = new ActiveXObject("Msxm12.XMLHTTP");
}catch (e){
try{
XMLHttpReq = new ActiveXOject("Microsoft.XMLHTTP");
}catch(e){
}
}
}
}
//发送请求函数
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange = processResponse; //指定响应函数
XMLHttpReq.send(null);
}
//处理返回信息函数
function processResponse(){
if(XMLHttpReq.readyState == 4){
if(XMLHttpReq.status == 200){
var res = XMLHttpReq.responseXML.getElementsByTagName("res")[0].firstChild.data;
window.alert(res);
}else{
window.alert("您所请求的页面有问题。");
}
}
}
//注册函数
function signUp(){
var uname = document.myform.uname.value;
var psw = document.myform.psw.value;
var psw2 = document.myform.psw2.value;
if(uname == ""){
window.alert("用户名不能为空");
document.myform.uname.focus();
return false;
}
else if(psw != psw2){
window.alert("两次密码不正确");
document.myform.psw.focus();
return false;
}
else{
sendRequest('signUp?uname='+ uname + '&psw=' +psw);
}
}
</script>
</head>
<body vLink ="#006666" link="#003366" bgColor="#E0F0F8">
<form action="" method="post" name="myform">
用户名:<input size="15" name="uname"><p>
密码:<input type="password" size="15" name="psw"><p>
重输密码:<input type="password" size="15" name="psw2"><p>
<input type="button" value="注册" onclick="signUp()">
</form>
</body>
</html>
new ActiveXOject("..."); 搜一下用的浏览器所对应的参数.
程序应该没有问题的.
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}//如果其它浏览器就用XMLHttpRequest
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
XMLHttpReq = new XMLHttpRequest();
//ActiveXObject
}else if(window.ActiveXOject)
{
try{
XMLHttpReq = new ActiveXObject("Msxm12.XMLHTTP");
}catch (e){
try{
XMLHttpReq = new ActiveXOject("Microsoft.XMLHTTP");
}catch(e){
}
}
}
}