Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Post array empty on axios POST

I'm trying out Vue 2.0 and axios and I've got a little issue. When I try to send a post request using axios to my post.php file the $_POST array is always empty.

Post function:

doPost: function() {
  console.log("post in progress")
  axios.post('api/post.php', {
      title: 'foo',
      body: 'bar',
      userId: 1
  })
  .then(response => {
    console.log(response)
    console.log(response.data)
    this.filter = response.data
  })
  .catch(e => {
    this.errors.push(e)
  })
}

post.php

<?php
header('Content-Type: application/x-www-form-urlencoded');


echo json_encode($_POST);
?>

The request is completed with status 200 but returns an empty object "[]"

Note: when I change the post endpoint to jsonplaceholder tool it works fine.

like image 580
Avi Avatar asked Jun 28 '17 10:06

Avi


1 Answers

I think you can try this, it should be the data-type problem.

var data = new FormData();
data.append('title', 'foo');
data.append('body', 'bar');

axios.post('api/post.php', data)
    .then(response => {
        console.log(response)
        console.log(response.data)
        this.filter = response.data
    })
    .catch(e => {
        this.errors.push(e)
});
like image 194
gy134340 Avatar answered Oct 13 '22 20:10

gy134340