Heim  >  Artikel  >  Backend-Entwicklung  >  javascript - php文件中执行print_r($_POST)时,显示Array(),为什么呢?

javascript - php文件中执行print_r($_POST)时,显示Array(),为什么呢?

WBOY
WBOYOriginal
2016-06-06 20:13:131474Durchsuche

<code> function createXML(){
                var xml = "<pets>";
                var options = document.getElementById("petTypes").childNodes;
                var option = null;
                for(var i = 0;i " + option.value + "";
                    }
                }
                xml = xml + "";
                return xml;
            }
            
            function sendPetTypes(){
                createXMLHttpRequest();
                var xml = createXML();
                var url = "PostingXMLExample.php?timeStamp=" + new Date().getTime();
                
                xmlHttp.open("POST",url,true);
                xmlHttp.onreadystatechange = handleStateChange;
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
                xmlHttp.send(xml);
            }</pets></code>

php文件中执行print_r($_POST)时,显示Array(),当执行echo file_get_contents("php://input")时,显示内容,为什么呢?请大神赐教

<code>function handleStateChange(){
                if(xmlHttp.readyState == 4){
                    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);
            }</code>

回复内容:

<code> function createXML(){
                var xml = "<pets>";
                var options = document.getElementById("petTypes").childNodes;
                var option = null;
                for(var i = 0;i " + option.value + "";
                    }
                }
                xml = xml + "";
                return xml;
            }
            
            function sendPetTypes(){
                createXMLHttpRequest();
                var xml = createXML();
                var url = "PostingXMLExample.php?timeStamp=" + new Date().getTime();
                
                xmlHttp.open("POST",url,true);
                xmlHttp.onreadystatechange = handleStateChange;
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
                xmlHttp.send(xml);
            }</pets></code>

php文件中执行print_r($_POST)时,显示Array(),当执行echo file_get_contents("php://input")时,显示内容,为什么呢?请大神赐教

<code>function handleStateChange(){
                if(xmlHttp.readyState == 4){
                    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);
            }</code>

<code> xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 </code>

看着好像是多了个; ???

因为在传递过来的时候,传递的是字符串,这个时候PHP帮你自动转成了数组,但是后面get_file_contents(php://input)这个是读取的原始数据流,这个时候就需要你自己进行转了。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn