日期:2014-05-17  浏览次数:20394 次

在ajax中用POST方法提交的XML串,服务器端如何回显到客户端?
符上原代码:
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN ">
<HTML>
  <HEAD>
    <TITLE>   New   Document   </TITLE>
    <META   NAME= "Generator "   CONTENT= "EditPlus ">
    <META   NAME= "Author "   CONTENT= " ">
    <META   NAME= "Keywords "   CONTENT= " ">
    <META   NAME= "Description "   CONTENT= " ">
  </HEAD>
<script>
var   xmlhttp;
function   createXmlRequest(){
if(window.ActiveXObject){
xmlhttp=new   window.ActiveXObject( "Microsoft.XMLHTTP ");
}else{
if(window.XMLHttpRequest){
xmlhttp=new   XMLHttpRequest();
}else{
alert( "error ");
}
}
var   url= "postxml.php? "+new   Date().getTime();
xmlhttp.open( "POST ",url,true);
xmlhttp.setRequestHeader( "Content-Type ", "application/x-www-form-urlencoded; ");
xmlhttp.onreadystatechange=show;
xmlhttp.send(createXml());
}
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> ";
}
}
alert(xml+ " </pets> ");
return   xml+ " </pets> ";

}
function   show(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
var   response=document.getElementById( "response ");
if(response.hasChildNodes()){
response.removeChild(response.childNodes[0]);
}
document.getElementById( "response ").appendChild(document.createTextNode(xmlhttp.responseText));
}
}
}
</script>
  <BODY>
    <h1> select   OPtion </h1>
    <form   action= "# ">
    <select   name= "s "   id= "petTypes "   multiple   >
    <option   value= "a "> afds </option>
    <option   value= "子子子 "> 子子子 </option>
    <option   value= "ccc "> cccc </option>
    <option   value= "声东击西test "> 声东击西test </option>
    </select>
    <input   type= "button "   value= "XMLPOST "   onclick= "createXmlRequest(); ">
    </form>
    Server   response
    <div   id= "response "> test </div>
  </BODY>
</HTML>


------解决方案--------------------
客户端:下面这个函数里面应该添加接收后的处理.
function show(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
var return_value = xmlhttp.responseXML;
//然后你再对返回的这个值进行处理.
}
}
服务器端:postxml.php中
根据你所获取的参数进行输出一个XML格式的东西.


我也是刚接触.如有不对,敬请谅解.