一个简单的Ajax与JSP示例 [英] A Simple AJAX with JSP example
问题描述
我想学习Ajax和JSP,我写了下面的code。这似乎并不奏效。请帮助:
这是我的configuration_page.jsp
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<冠军> JSP页面< /标题>
<脚本类型=文/ JavaScript的>
函数loadXMLDoc()把
{
VAR XMLHTTP;
。无功配置=的document.getElementById('configselect')值;
VAR URL =get_configuration.jsp;
如果(window.XMLHtt prequest)
{
XMLHTTP =新XMLHtt prequest();
}
其他
{
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能()
{
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
{
的document.getElementById(myDiv)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,URL,真正的);
xmlhttp.send();
}
< / SCRIPT>
< /头>
<身体GT;
< H2 ALIGN =中心>保存的配置< / H>
选择一个配置来运行:
<选择名称=configselectWIDTH =10>
<选项选择的值=选择>选择< /选项>
<期权价值=配置Config1> CONFIG1< /选项>
<期权价值=配置Config2> CONFIG2< /选项>
<期权价值=配置Config3> CONFIG3< /选项>
< /选择>
<按钮式=按钮的onclick =loadXMLDoc()把'>提交< /按钮>
< DIV ID =myDiv>
< H4>索取资料此处< / H4>
< / DIV>
< /身体GT;
< / HTML>
这是我get_configuration.jsp,而我试图从AJAX code以上的访问:
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<冠军> JSP页面< /标题>
< /头>
<身体GT;
< H4>眉..< / H4>
< /身体GT;
< / HTML>
您正在做错误的configuration_page.jsp文件。 这里,在这个文件中,函数loadXMLDoc()把的2号线应该是这样的:
无功配置= document.getElementsByName('configselect')值;
因为你只声明了名称
属性在<选择>
标记。所以,你应该通过名字得到这个元素。
在纠正这个,它将运行没有任何JavaScript错误
I am trying to learn AJAX with JSP and I have written the following code. This does not seem to be working. Kindly help:
This is my configuration_page.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var config=document.getElementById('configselect').value;
var url="get_configuration.jsp";
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2 align="center">Saved Configurations</h2>
Choose a configuration to run:
<select name="configselect" width="10">
<option selected value="select">select</option>
<option value="Config1">config1</option>
<option value="Config2">config2</option>
<option value="Config3">config3</option>
</select>
<button type="button" onclick='loadXMLDoc()'> Submit </button>
<div id="myDiv">
<h4>Get data here</h4>
</div>
</body>
</html>
This is my get_configuration.jsp which I am trying to access from the AJAX code above:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h4>Mee..</h4>
</body>
</html>
You are doing mistake in "configuration_page.jsp" file. here in this file , function loadXMLDoc() 's line number 2 should be like this:
var config=document.getElementsByName('configselect').value;
because you have declared only the name
attribute in your <select>
tag. So you should get this element by name.
After correcting this, it will run without any JavaScript error
这篇关于一个简单的Ajax与JSP示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!