Ajax向服务器端发送Xml数据,提示404错误
Ajax向服务器端发送Xml数据,提示404错误,好像是没有找到url地址,我的url是这样定义的 var url="PostingXMLExample?timeStamp=" + new Date().getTime();
使用名为PostingXMLExample.java的Servlet文件进行捕获,但是好像无法捕获到,是不是Servlet文件的放置目录有什么要求,还是其它什么问题。
我的html文件代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function createXML(){
var xml="<pets>";
var optional=document.getElementById("petTypes").childNodes;
var option=null;
for(var i=0;i<optional.length;i++){
option=optional[i];
if(option.selected){
xml=xml+"<type>"+option.value+"<\/type>";
}
}
xml=xml+"<\/pets>";
return xml;
}
function sendPetTypes(){
createXMLHttpRequest();
var xml=createXML();
var url="PostingXMLExample?timeStamp=" + new Date().getTime();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(xml);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
alert(xmlHttp.status);
if(xmlHttp.status==200){
parseResults();
}
}
}
function parseResults(){
var responseDiv=document.getElementById("serverResponse");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText=document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>
<body>
<h1>select the type of pets in your home</h1>
<form action="">
<select id="petTypes" size="6">
<option value="cat">cat</option>
<option value="dog">dog</option>
<option value="fish">fish</option>
<option value="bird">bird</option>
<option value="hamster">hamster</option>
<option value="rabbit">rabbit</option>
</select>
<br /><br />
<input type="button" value="submit pets" onclick="sendPetTypes();" />
</form>
<h2>Server Response</h2>
<div id="serverResponse"></div>
</body>
</html>