繁体   English   中英

为什么在发布数据请求后,PHP中的$ _POST数组为空

[英]Why is $_POST array empty in PHP after an a request with post data

我向页面getremote.php发出了带有发布数据的发布请求,但是$ _POST数组似乎为空。 如果有人能告诉我我做错了,将不胜感激。

发出请求的javascript代码是

var postdata = "Content-Type: application/x-www-form-urlencoded\n\nedits=" + this.createEditXMLtext(this.editXMLstruct);
 var xmlhttp;
 if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
 else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
  dispmes("processing edits");
 xmlhttp.open("POST",userProfile.homeurl + "?remoteurl=" + userProfile.homeurl + "&cmdeditprofile&password=password",false);

 xmlhttp.send(postdata);

 var response = xmlhttp.responseXML;

其中this.createEditXMLtext(this.editXMLstruct)仅创建一个字符串

我以前没有遇到过这个问题,并且似乎没有与其他发布过类似问题的人相同的解决方案。 在userProfile.homeurl +“处的php代码是

header("Content-type: text/xml");
 $query = '';                  
    foreach( $_POST as $key => $value ){ 
  $query .= "$key=$value&";
 }
 echo do_post_request($_GET['remoteurl'] . $qstring,$query);

但是字符串$ query始终为空-我通过在文件底部添加echo $ query进行了检查

您传递给send()的值应该是整个帖子正文,并且其中包含标头。 当该主体到达PHP时,它将无法将其解析为编码的表单数据。

而是通过调用setRequestHeader()设置数据类型。

 //create the postdata, taking care over the encoding
 var postdata = "edits=" + encodeURI(this.createEditXMLtext(this.editXMLstruct));

 //let server know the encoding we used for the request body
 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

 //and here we go 
 xmlhttp.send(postdata);

我从未见过这样做的方法,请尝试通过XMLHttpRequest.setRequestHeader()将头与POST主体分开设置,如下所示:

var postdata = "edits=" + this.createEditXMLtext(this.editXMLstruct);
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
dispmes("processing edits");
xmlhttp.open("POST", userProfile.homeurl + "?remoteurl=" + userProfile.homeurl + "&cmdeditprofile&password=password",false);
xmlhttp.send(postdata);
var response = xmlhttp.responseXML;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM