axios POST上的PHP Post数组为空

2022-01-01 00:00:00 axios php javascript

我正在试用 Vue 2.0 和 axios,但遇到了一个小问题.当我尝试使用 axios 向我的 post.php 文件发送 post 请求时,$_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.

发布功能:

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);
?>

请求以状态 200 完成,但返回一个空对象[]"

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

注意:当我将 post 端点更改为 jsonplaceholder 工具时,它工作正常.

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

推荐答案

我觉得你可以试试这个,应该是数据类型的问题.

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)
});

相关文章