Javascript + PHP $ _POST array is empty

When I try to send a POST request via xmlhttp.open("POST", "url", true)

(javascript) to the server, I get empty $_POST array

.

Firebug shows data is being sent. Here is the line of data from the Firebug: a=1&q=151a45a150...

. But $_POST['q']

it returns nothing.

I wonder what file_get_contents('php://input')

my data (line above) has, but PHP somehow doesn't recognize it. Tried both $ _POST and $ _REQUEST, nothing works.

Headers sent:

POST /test.php HTTP/1.1
Host: website.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401        Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://website.com/
Content-Length: 156
Content-Type: text/plain; charset=UTF-8
Pragma: no-cache
Cache-Control: no-cache

      

Thanks for any suggestions.

+2


a source to share


4 answers


It looks like you are missing the correct Content-Type header. This is required for POST requests:



xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

      

+4


a source


Send message

Content-Type: application/x-www-form-urlencoded

      



instead text/plain

+2


a source


You should write it like this:

xmlhttp.open("POST", "script.php", true);
xmlhttp.send("foo=bar&answer=42");

      

0


a source


Just spent hours trying to find a fix for this very problem.

I made an idiotic mistake by concatenating multiple lines that I wanted to be parameters, and THEN, naming encodeURIComponent

throughout the whole batch. This of course meant that

foo=bar&this=that

      

became

foo%3Dbar%26this%3Dthat

      

which is of course gibbering at the PHP script. While I doubt there could be many people who would do something as stupid as this, I hope it saves someone the headache I just gave myself ....

0


a source







All Articles