2010-10-23 38 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