【问题标题】:Axios to php api - POST form emptyAxios 到 php api - POST 表单为空
【发布时间】:2020-01-03 09:15:56
【问题描述】:

我确信这是一个容易的,希望它很容易。在设置 axios(使用 VUE)并尝试发布任何内容时,没有收到 POST。邮递员工作正常,但我的带有 Axios 的 vue 应用程序没有,POST 数组到达空。

devtools 中的传出请求负载具有变量(目前只是'test'='test。

来自 Vue

      this.$axios.post("http://localhost/api/process.php?action=addEntry", {
        test:"test"
      }).then(function (response) {
          alert(JSON.stringify(response));
        })
        .catch(function (error) {
          alert("error");
          alert(JSON.stringify(error));
      });
      return
    },

来自 PHP Api - process.php

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: User-Agent, Sec-Fetch-Mode, Referer, X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
    header("Access-Control-Allow-Methods: POST");
    header('Content-Type: application/json;charset=UTF-8'); 

echo (var_dump($_POST['test']));
echo (var_dump($_POST));

输出

POST 不可用。

响应

标题

【问题讨论】:

  • 在哪个方法中添加你的axios代码应该是mounted方法如果是这样尝试在你的组件中导入axios并写axios.post('')
  • 你能分享你的 .vue 文件吗?我觉得少了点什么!

标签: php post axios


【解决方案1】:

试试这个

axios.post("urlapi", 
                 data: {
                    foo: 'bar', // This is the body part
                  }, {
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(function(res){
                console.log(res);
            }).catch();

这对我有用。

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 2021-12-29
    • 2020-04-06
    • 2019-10-19
    • 2020-11-03
    • 1970-01-01
    • 2018-07-02
    • 2014-02-22
    • 1970-01-01
    相关资源
    最近更新 更多