PHP Post数组在axios POST上为空 [英] PHP Post array empty on axios POST

查看:320
本文介绍了PHP Post数组在axios POST上为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

注意:当我将发布端点更改为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)
});

这篇关于PHP Post数组在axios POST上为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆