Axios转PHP API-POST表单为空

时间:2019-08-30 06:04:57

标签: php post axios

我确定这很容易,希望很容易。在设置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不可用。

响应 response

标题

headers

1 个答案:

答案 0 :(得分:0)

尝试

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

这对我有用。

相关问题