• php+ajax+xml POST提交方式 全新时代


    <html>
        <head>
            <script type="text/javascript" src="json.js"></script>
            <script>
                //创建AJAX XMLHttpRequest对象
                var xmlHttp = false;
                try{
                    xmlHttp = new Activexobject("Msxml2.XMLHTTP");
                }catch(e){
                  try{
                      xmlHttp = new Activexobject("Microsoft.XMLHTTP");
                  }catch(e2){
                      xmlHttp = false;
                  }
                }
                
                if(!xmlHttp && typeof XMLHttpRequest != 'undefined'){
                    xmlHttp = new XMLHttpRequest();
                }
    
                
                //POST方式
                function callServer2(){
                    var city = document.getElementById("city").value;
                    var state = document.getElementById("state").value;
                    if((city == null) || (city == "")) return;
                    if((state == null) || (state == "")) return;
                    
                    var xmlString ="<profile><test>" +
                    "<city>" + escape(city) + "</city>" +
                    "<state>" + escape(state) + "</state>" +
                    "</test><test>" +
                    "<city>1111</city>" +
                    "<state>2222</state>" +
                    "</test></profile>"
                    
                    var url = "response.php?timeStamp=" + new Date().getTime();
                    //alert(url);
                    xmlHttp.open("POST",url,true);
                    xmlHttp.onreadystatechange = updatePage;
                    xmlHttp.setRequestHeader("Content-Type","text/xml");
                    xmlHttp.send(xmlString);
    
                }
                
            //回调处理函数
            function updatePage(){
             if(xmlHttp.readyState == 4){
                 if(xmlHttp.status == 200){
                      
                      //IE6中是要创建对象 ActiveXObject("Microsoft.XMLDOM") 对responseXML对象进行加载后方可用getElementsByTagName进行解析的。
                      /*
                      要将responseXML这个片段,用loadXML()函数,这样才能当作XML文档来解析。否则取得的就是一个用XML格式好的字符串而不是XML模型。因为取回的是一段字符串,而不是XMLDOM模型。用DOM.loadXML(取回的字符串)
                      */
                      var xmlDoc=new ActiveXObject("MicroSoft.XMLDOM"); 
                    xmlDoc.loadXML(xmlHttp.responseText);
                    
                    
                      var test = xmlDoc.getElementsByTagName("test");
                    alert(test.length);
                    alert(test[0].firstChild.firstChild.data);
                    alert(test[1].firstChild.firstChild.data);
    document.getElementById("zipcode").value = test[0].firstChild.firstChild.data;
                 }
             }
            }
                
            </script>
        </head>
        
        <body>
            <form>
                <p>City: <input type="text" name="city" id="city" size="25" onChange="callServer2();"></p>
                <p>State: <input type="text" name="state" id="state" size="25" onChange="callServer2();"></p>
                <p>Zip Code:<input type="text" name="zipcode" id="zipcode" size="5"></p>
            </form>
        </body>
        
    </html>

    response.php

    <?
    
    echo file_get_contents("php://input");
    
    ?>
    

     注意点~!

    1. xmlHttp.setRequestHeader("Content-Type","text/xml");设置xml格式

    2. var xmlDoc=new ActiveXObject("MicroSoft.XMLDOM");
        xmlDoc.loadXML(xmlHttp.responseText);

        返回的xml是字符串格式需要转换成XMLDOM模型,否则xmlDoc.length为0,获取不到xml节点值。

    3. dom获取方式,test[0].firstChild.firstChild.data

  • 相关阅读:
    hdu 3951 Coin Game
    hdu 1273 漫步森林
    hdu 2082 找单词
    kmp算法(模板)
    CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上学期周总结
    大二上每日总结
  • 原文地址:https://www.cnblogs.com/simpledev/p/3043120.html
Copyright © 2020-2023  润新知