Why $ _POST pool is empty in PHP after request with post data

I am making an email request to the getremote.php page with the post data, but the $ _POST array seems empty. I would be grateful if someone would tell me what I did wrong.

Javascript code for request

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; 

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

I didn’t have this problem before and didn’t seem to have the same solution as other people who posted similar problems. PHP code in 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 $ query line is always empty - I checked it by adding echo $ query to the end of the file

+2
source share
2 answers

The value you pass to send () must be the whole body of the message, and you include the header in it. When this body reaches PHP, it will not be able 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); 
+4
source

I never saw this done, try setting your header separately from the POST body using XMLHttpRequest.setRequestHeader() , for example:

 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; 
+1
source

Source: https://habr.com/ru/post/895105/


All Articles