2010-10-23 106 views
2

我向发布数据的页面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); 

但是字符串$查询始终是空的 - 我通过添加回声$查询到的文件

+0

数据类型设置你100%肯定'POST'是空的? print_r($ _ POST);'产生了什么? – 2010-10-23 11:22:10

+0

我想,标题信息必须用两个\ r \ n而不是\ n分开。 HTTP 1.1规范: 通用消息=开始线 *(消息头CRLF) CRLF [消息体] 开始线=请求行|状态行 CRLF = \ r \ n – DrDol 2010-10-23 11:23:25

+0

您当然应该考虑转到JavaScript框架(如jQuery,Dojo或YUI)来处理您的AJAX请求。 – 2010-10-23 14:23:07

回答

4

传递给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); 
+0

嗨,感谢您的帮助 - 现在完美的作品。 – David 2010-10-23 20:07:02

1

的底部我从来没有见过它检查它这样做的方式,尝试通过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; 
+0

嗨,感谢您的帮助 - 它现在正在努力:) – David 2010-10-23 20:07:32

相关问题