Unable to post to a php server from web browser

This is kind of unusual.

I’m using Fedora 28 and have PHP 7.2 installed on the machine. I’ve used the following command to start a local http server for testing php -s localhost:8000/

I’m able to post to php scripts which live in the directory where I start the php server using curl (curl -d foo=bar localhost:8000/somephp.php)

However, when I use a browser and javascript to do an ajax post to the script, it returns an empty array.

I get a 200 when I post to the script and I’m using very vanilla js to complete the ajax post.

Any suggestions on where to start with one?

Well it looks like an implementation issue, how is the javascript sending data to the server and how is the server receiving it? If it’s sending in through POST and the server is looking at GET then it’s like nothing was sent.

Hi @cleong

I’m sending the request as a post and attempting to access the $_POST variable.

The php server doesn’t appear to have trouble with receiving post (ie I can use the curl command to post to it). It only seems to occur when I use Javascript.

It appears the JS can reach the php file fine (the 200 I receive) it’s just the data isn’t being captured.

My JS is basically the following:

var xhttp = new XMLHttpRequest();
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();  

And I attach it to an eventHandler.

1 Like

Have you tried to dump the http request? See what the server is receiving.

Can you give a more comprehensive fragment of your script because from what you’ve given the curl request is indeed POST but the ajax one is a GET request.

Could you try and post a snippet where you read your post on the server?

This is old, but oh well.

Just realized you’re sending a get request.

Do this

xhttp.open("POST", "yourscript.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send('your=postdata&goes=here');
1 Like