0

I am new to express framework, in my application I use axios to send HTTP requests to a php server. I am using following code to send POST requests to a php page with parameters 'username' and 'password' as described in the axios documentation. But my php server does not receive any POST parameters, because

echo($_POST['username']);

doesn't return anything. Can anyone tell me what I am doing wrong here & instruct me to correct this without changing the php files? following is the code which I used to send HTTP POST requests

axios.post('http://localhost/test/login.php', {
    username: 'test',
    password: 'user@test'
  })
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });
Oshada
  • 112
  • 4
  • 13
  • Possible duplicate of [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – Dez Aug 27 '18 at 19:30

1 Answers1

2

See the documentation for axios:

By default, axios serializes JavaScript objects to JSON.

PHP doesn't automatically parse JSON formatted requests.

You'll need to either send data in a format that PHP will decode into $_POST as described in the documentation linked above, or write your PHP so it can handle a JSON encoded request (as described in this answer).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335