【问题标题】:How to post array of object to PHP using axios?如何使用 axios 将对象数组发布到 PHP?
【发布时间】:2023-03-26 12:00:02
【问题描述】:

let dataArray = [
  {
    fname: 'name #1',
    choice: 'choice #1',
  },
  {
    fname: 'name #2',
    choice: 'choice #2',
  },
  // more data could be appended here
];

我的 VueJS 表单中有一个如上所述的数据结构。如何将这些数据发送到我的 PHP 后端并将其保存到数据库中?

到目前为止,我的尝试毫无用处。我正在使用 axios 将我的数据发布到我的 PHP 后端。我尝试过使用 FormData() 和 JSON.stringify 以及在 PHP 端获取数据的各种方法。

据我了解,axios 会在内部处理 json 格式。

这是我在应用中的数据结构:

data: {
  enteredDataArray: [{
    fname: '',
    radioVal: ''
  }]
}

onSubmit(evt){
  evt.preventDefault();
  axios.post('api.php', app.enteredDataArray)
  .then(res => console.log(res))
  .catch(err => console.log(err))
}
$data = $_POST;

【问题讨论】:

  • 有什么错误吗?请提供有关您的问题的一些详细信息:)

标签: javascript php arrays vue.js axios


【解决方案1】:

PHP $_POST 需要一个 FormData。

如果你想在 PHP 中接收 JSON,$_POST 不是办法,你可以这样做:

$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);
var_dump($input)

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2021-11-15
    • 2020-12-15
    • 2019-04-12
    • 2021-11-23
    • 2018-06-20
    • 2020-10-23
    • 2019-06-18
    • 2023-02-04
    相关资源
    最近更新 更多