这是我的源代码,请大家帮忙看看,我读到的怎么都是空值。是不是方法错了?
<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 options=document.getElementById("petTypes").childNodes;
var option=null;
for(var i=0;i<options.length;i++){
option=options[i];
if(option.selected){
xml=xml+"<type>"+option.value+"<\/type>";
}
}
xml=xml+"<\/pets>"+"\n";
alert(xml);
return xml;
}
function sendPetTypes(){
createXMLHttpRequest();
var xml=createXML();
var url="/wms/wmtsppdmul.html?method=ajax&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(){
alert(xmlHttp.readyState) ;
if(xmlHttp.readyState==4){
alert(xmlHttp.status) ;
if(xmlHttp.status==200){
alert("");
parseResults();
}
}
}
function parseResults(){alert(xmlHttp.responseText);
var responseDiv=document.getElementById("serverResponse");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText=document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
public void ajax(HttpServletRequest request,HttpServletResponse response) throws Exception {
// request.setCharacterEncoding("GB2312");
// response.setContentType("text/html;charset=GB2312");
String x=request.getParameter("");
String xml=readXMLFromRequestBody(request);
Document xmlDoc=null;
try{System.out.println("=============");
xmlDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
}catch(ParserConfigurationException e){
System.out.println("ParserConfigurationException:"+e);
}catch(SAXException e){
System.out.println("SAXException:"+e);
}System.out.println("=====111111========");
NodeList selectedPetTypes=xmlDoc.getElementsByTagName("type");
System.out.println("=============");
String type=null;
String responseText="Selected Pets:";
System.out.println("=============");
for(int i=0;i<selectedPetTypes.getLength();i++){
type=selectedPetTypes.item(i).getFirstChild().getNodeValue();
responseText=responseText+""+type;
}
System.out.println("=====333333333========");
response.setContentType("text/xml");
response.getWriter().print(responseText);
}
private String readXMLFromRequestBody(HttpServletRequest request){
System.out.println("====2222222=========");
StringBuffer xml=new StringBuffer();
String line=null;
try{
BufferedReader reader=request.getReader();
System.out.println("wwwwwww======"+reader.readLine());
while((line=reader.readLine())!=null){
xml.append(line);
}
}catch(Exception e){
System.out.println("Error reading XML:"+e.toString());
}
System.out.println("efadsfdafd============="+xml.toString());
return xml.toString();
}