0
 function getXmlHttpRequestObject()
            {
                var xmlHttp = false;
                if (window.XMLHttpRequest)
                {
                    return new XMLHttpRequest(); //To support the browsers IE7+, Firefox, Chrome, Opera, Safari
                }
                else if(window.ActiveXObject)
                {
                    return new ActiveXObject("Microsoft.XMLHTTP"); // For the browsers IE6, IE5 
                }
                else
                {
                    alert("Error due to old verion of browser upgrade your browser");
                }
            }

            var xmlhttp = new getXmlHttpRequestObject(); //xmlhttp holds the ajax object

            function servletPost() {
                if(xmlhttp) { 
                   var txt = document.getElementById("txtname");
                    var txtname=document.URL;
                    xmlhttp.open("POST","ServletPost",true);
                    xmlhttp.onreadystatechange = handleServletPost;
                    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xmlhttp.send("txtname=" + txtname); 

                }
            }

            function handleServletPost() {
                if (xmlhttp.readyState == 4) {
                    if(xmlhttp.status == 200) {
                        document.getElementById("message").innerHTML=xmlhttp.responseText; 
                    }
                    else {
                        alert("Ajax calling error");
                    }
                }
            }

这是我的代码函数 servletPost() 正在将数据发送到 servlet 但只有 txtname 没有其他值会发送到 servlet 我该如何解决它请帮忙

4

2 回答 2

1

添加变量中的所有参数并发送。前任。

var params = "txtname=" + txtname + "&name=user";
xmlhttp.send("txtname=" + txtname);
于 2013-04-11T07:01:50.010 回答
0

在您的代码中,我只看到您只发送一个参数 txtname。

如果要发送更多参数,则在其间添加“&”。

params = "txtname=" + txtname + "&txt=" + txt;
于 2013-04-11T07:27:50.757 回答