从JSP读取javascript响应 我是学习AJAX的新手,并且一直在学习第一个程序。试图调试,但无法调试。

从JSP读取javascript响应 我是学习AJAX的新手,并且一直在学习第一个程序。试图调试,但无法调试。,javascript,html,ajax,jsp,response,Javascript,Html,Ajax,Jsp,Response,下面是我的代码片段 ----------input-ajax.html------------------- <!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; chars

下面是我的代码片段

----------input-ajax.html-------------------

<!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>HTML PAGE</title>
<script type="text/javascript">
    var request=null;
    function createRequest(){
        try{
            request= new XMLHttpRequest();
        )catch(e){
            try{
                request=new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                try{
                    request= new ActiveXObject("Microsoft.XMLHTTP");
                }catch(failed){
                    request=null;
                }
            }
        }
        if(request==null){
            alert("Error Creating Request Object");
        }
    }
    function getName(){
        alert("getname called");
        createRequest();
        var url = "showName-ajax.jsp";
        request.open("POST",url,true);
        alert("before");
        request.onreadystatechange = updatePage;
        alert("after");
        request.send(null);
    }
    function updatePage(){
        //var newName= request.getResponseHeader("r1");
        var newName= request.responseText;
        var name1 = document.getElementById("name");
        replaceText(name1,newName);
    }
    function replaceText(el, text) {
          if (el != null) {
            clearText(el);
            var newNode = document.createTextNode(text);
            el.appendChild(newNode);
          }
    }

    function clearText(el) {
        if (el != null) {
            if (el.childNodes) {
              for (var i = 0; i < el.childNodes.length; i++) {
                var childNode = el.childNodes[i];
                el.removeChild(childNode);
              }
            }
        }
    }
</script>
</head>
<body>
<h1>WELCOME <span id="name"> GUEST</span></h1>
<form method="POST">
<input type="text" name="t1">
<input type="button" value="Show Name" onclick="getName();"/>
</form>
</body>
</html>

HTML页面
var请求=null;
函数createRequest(){
试一试{
请求=新的XMLHttpRequest();
)捕获(e){
试一试{
请求=新的ActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
请求=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(失败){
请求=null;
}
}
}
if(请求==null){
警报(“创建请求对象时出错”);
}
}
函数getName(){
警报(“调用getname”);
createRequest();
var url=“showName ajax.jsp”;
打开(“POST”,url,true);
警惕(“之前”);
request.onreadystatechange=updatePage;
警报(“之后”);
请求发送(空);
}
函数updatePage(){
//var newName=request.getResponseHeader(“r1”);
var newName=request.responseText;
var name1=document.getElementById(“名称”);
替换文本(名称1,新名称);
}
函数替换文本(el,文本){
如果(el!=null){
明文(el);
var newNode=document.createTextNode(文本);
el.appendChild(newNode);
}
}
函数明文(el){
如果(el!=null){
if(el.childNodes){
对于(变量i=0;i
----------showName-ajax.jsp------------

<!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>HTML PAGE</title>
<script type="text/javascript">
    var request=null;
    function createRequest(){
        try{
            request= new XMLHttpRequest();
        )catch(e){
            try{
                request=new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                try{
                    request= new ActiveXObject("Microsoft.XMLHTTP");
                }catch(failed){
                    request=null;
                }
            }
        }
        if(request==null){
            alert("Error Creating Request Object");
        }
    }
    function getName(){
        alert("getname called");
        createRequest();
        var url = "showName-ajax.jsp";
        request.open("POST",url,true);
        alert("before");
        request.onreadystatechange = updatePage;
        alert("after");
        request.send(null);
    }
    function updatePage(){
        //var newName= request.getResponseHeader("r1");
        var newName= request.responseText;
        var name1 = document.getElementById("name");
        replaceText(name1,newName);
    }
    function replaceText(el, text) {
          if (el != null) {
            clearText(el);
            var newNode = document.createTextNode(text);
            el.appendChild(newNode);
          }
    }

    function clearText(el) {
        if (el != null) {
            if (el.childNodes) {
              for (var i = 0; i < el.childNodes.length; i++) {
                var childNode = el.childNodes[i];
                el.removeChild(childNode);
              }
            }
        }
    }
</script>
</head>
<body>
<h1>WELCOME <span id="name"> GUEST</span></h1>
<form method="POST">
<input type="text" name="t1">
<input type="button" value="Show Name" onclick="getName();"/>
</form>
</body>
</html>


但是当我运行程序(单击按钮)时,什么都没有发生。甚至调用getName函数的警告消息也没有出现。请帮助!!

输入错误:

)catch(e){
应该是

}catch(e){
这将在浏览器的控制台中显示为错误。如果事情不正常,请务必首先检查该错误。

您有a)而不是a}此处:

try{
    request= new XMLHttpRequest();
)catch(e){
所以你的函数永远不会被定义

change )catch(e){  to }catch(e){
但是 为什么每次单击按钮时都要创建请求对象? 您只需要在页面加载事件中调用“createRequest()”一次