2011-11-18 21 views
1

我正在使用XDomainRequest從IE發出POST請求。 XDomainRequest不會在其標題中發送Content-Type,它是not possible to set a custom header。我認爲這會導致PHP不把post變量放在$_POST如何獲取錯誤標題的POST參數

我可以看到參數發送如果我使用file_get_contents("php://input")但我不知道如何正確解析它們而不會破壞它們。有沒有辦法強制PHP收集POST參數?或者我怎樣才能安全地獲得變量?

我使用this transport得到XDomainRequest在jQuery和撥打這個電話:

jQuery.post('http://domain.de/io/login', '[email protected]&password=xxx', function(response) 
{ 
    if (console) console.log(response); 
    alert('response: ' + objectToString(response)); 
}); 

在PHP端我有:

print_r($_REQUEST, true); 
echo file_get_contents("php://input"); 

正常工作與Firefox瀏覽器。但Firefox不使用傳輸。

+5

POST變量不會奇蹟般地消失,即使某些標題不存在或不存在。請顯示代碼如何發出請求。 – CodeCaster

+1

'我認爲這會導致PHP不把post變量放在$ _POST中。「這個錯誤的斷言是你的薄弱環節。事實回來! :) –

+0

切出一些示例代碼並不容易。但是怎麼可能php://輸入包含變量而$ _REQUEST沒有?我可以看到的唯一區別是我從'getallheaders()' – PiTheNumber

回答

3

@hakre:非常感謝。你document描述了這裏沒有人belives:

請求的主體是根據 Content-Type頭進行解釋。

它也提到parse_str(),這正是我所需要的。

這裏我的代碼來填充$ _ POST,如果它不是由PHP設置:

if (count($_POST) == 0) 
{ 
    $request_body = file_get_contents("php://input"); 
    parse_str($request_body, $_POST); 
} 

我很樂意給你這個名聲,但我不知道怎麼辦。無論如何,你剛從我身上獲得了+50分,所以我希望這樣可以。

-2

正如我所用的jQuery.post()手冊看到,數據參數必須以下面的格式:

jQuery.post('http://domain.de/io/login', {email: "[email protected]", password: "xxx"}, function(response) 
{ 
    if (console) console.log(response); 
    alert('response: ' + objectToString(response)); 
}); 
+0

從手冊:「地圖或字符串」。與Firefox一起工作良好。 – PiTheNumber