当前位置:首页 > 操作系统 > Ios

javascript – 在postios POST上PHP发布数组为空

我正在尝试Vue 2.0和axios,我有一点问题.
当我尝试使用axios向post.php文件发送post请求时,$_POST数组始终为空.

发布功能:

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完成,但返回空对象“[]”

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

解决方法:

我想你可以尝试这个,它应该是数据类型的问题.

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

axios.post('api/post.php', {
     data: data
    })
    .then(response => {
        console.log(response)
        console.log(response.data)
        this.filter = response.data
    })
    .catch(e => {
        this.errors.push(e)
});

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!