How to get POST parameters with the wrong header

I am making a POST request from IE using XDomainRequest . XDomainRequest does not send a Content-Type to its header, and cannot set a custom header . I think this causes PHP to not post post variables in $_POST .

I see that the parameters are sent if I use file_get_contents("php://input") , but I'm not sure how to parse them correctly without breaking them. Is there a way to get PHP to collect POST parameters? Or how can I get the variables safely?

I use this transport to get XDomainRequest in jQuery and make this call:

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

On the PHP side, I have:

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

Works great with Firefox. But Firefox does not use transport.

+1
source share
2 answers

@hakre: Thank you very much. Your document describes that no one here believes:

The request body must be interpreted in accordance with the Content-Type Header.

He also mentions parse_str (), which does exactly what I need.

Here is my code to populate $ _POST if it has not been installed by PHP:

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

I would like to give you a reputation for this, but I do not know how to do it. Anyway, you just got 50 from me, so I hope everything is in order.

+3
source

As I can see in the jQuery.post() manual, the data parameter should be in the following format:

 jQuery.post('http://domain.de/io/login', {email: " test2@stuff.de ", password: "xxx"}, function(response) { if (console) console.log(response); alert('response: ' + objectToString(response)); }); 
-2
source

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


All Articles