admin管理员组文章数量:1403069
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\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) 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
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\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) 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
Share Improve this question asked Oct 23, 2010 at 11:19 DavidDavid 331 silver badge4 bronze badges 3-
Are you 100% sure
POST
is empty? What does aprint_r($_POST);
yield? – Pekka Commented Oct 23, 2010 at 11:22 - I guess that the Header information must be separated from the body with two \r\n and not only \n. HTTP 1.1 specification: generic-message = start-line *(message-header CRLF) CRLF [ message-body ] start-line = Request-Line | Status-Line CRLF = \r\n – DrDol Commented Oct 23, 2010 at 11:23
- You should certainly consider going to a javascript framework like jQuery, Dojo, or YUI to handle your AJAX requests. – Mitch C Commented Oct 23, 2010 at 14:23
2 Answers
Reset to default 4The 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);
I've never seen it done this way, try setting your header separately from the POST body via XMLHttpRequest.setRequestHeader()
, like this:
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;
本文标签: javascriptWhy is POST array empty in PHP after an a request with post dataStack Overflow
版权声明:本文标题:javascript - Why is $_POST array empty in PHP after an a request with post data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744337621a2601284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论