duanhan7001 2010-10-23 11:19
浏览 76
已采纳

为什么在带有后期数据的请求后,PHP中的$ _POST数组为空

I make a post request to the the page getremote.php with post data but the $_POST array seems to be empty. Would be grateful if anyone can tell me what I've done wrong.

The javascript code to make the request is

var postdata = "Content-Type: application/x-www-form-urlencoded

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");
   }
  dispmes("processing edits");
 xmlhttp.open("POST",userProfile.homeurl + "?remoteurl=" + userProfile.homeurl + "&cmdeditprofile&password=password",false);

 xmlhttp.send(postdata);

 var response = xmlhttp.responseXML;

where this.createEditXMLtext(this.editXMLstruct) simply creates a string

I haven't had this problem before and don't seem to have the same solution as other people who have posted similar problems. The php code at userProfile.homeurl + " is

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

but the string $query is always empty - I checked it by adding echo $query to the bottom of the file

  • 写回答

2条回答 默认 最新

  • drxp993551 2010-10-23 11:25
    关注

    The value you pass to send() should be the entire post body, and you've included a header in it. When that body reaches PHP, it will fail to parse it as encoded form data.

    Instead, set the data type by calling 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致