【问题标题】:Passing params through axios post call通过 axios post call 传递参数
【发布时间】:2017-07-02 13:22:49
【问题描述】:

我目前正在使用 vue-resource 在我的 webpack 项目中执行一些 ajax 调用。一旦我进行“get”调用,所有的工作都很好,但是当我尝试发布一个帖子时,我的 PHP 没有得到任何参数,如果我尝试执行 var_dump($_POST) 结果是一个空数组。 我的代码很基础:

let data = {req: 'req_tipe', ch: 001 };
this.$http.post('compute.php', data).then(response => {
    //do stuff with response if ok             
    },
    response => {
    //do stuff about error
    });

我不知道我错过了什么

更新: 我对 axios 和 vue-axios 做了同样的事情,但我的问题仍然存在: 没有帖子参数

let data = {req: 'req_tipe', ch: 001 };
this.axios.post('compute.php', data).then(response => {
    //do stuff with response if ok             
    },
    response => {
    //do stuff about error
    });

【问题讨论】:

  • vue-resource 已停用。用 AXIOS 替换它。
  • 这就是我所缺少的!!
  • 你错过了什么?
  • 如 M U 所指,vue-resource 已停用
  • 它已被淘汰,但您仍然可以使用上一个版本...

标签: javascript ajax post vue.js axios


【解决方案1】:

也许你可以改变如下:

let data = new FormData();
data.append('req','req tipe');
data.append('ch','001');
this.axios.post('compute.php', data).then(response => {
//do stuff with response if ok             
},
response => {
//do stuff about error
})

试一试,或查看issues 318了解更多信息。

【讨论】:

    【解决方案2】:

    问题是,当您将对象作为数据发送时,您使用 Content-Type "application/json" 执行 POST 请求,这是 Axios 中的默认设置。

    PHPS $_POST 仅在请求以 application/x-www-form-urlencoded 作为内容类型并且数据作为 Querystring 的情况下有效。

    当使用像 qs 这样的库时,以下应该可以工作:

    var qs = require('qs');
    axios.post('/foo', qs.stringify({ 'bar': 123 });
    

    当输入数据是查询字符串时,axios 会自动使用正确的内容类型。

    您也可以使用 json 数据,但是您必须将 PHP 后端更改为以下内容:

    $data = json_decode(file_get_contents('php://input'), true);
    print_r($data);
    

    像 Symphony 这样的一些框架可以自动处理这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2021-10-13
      • 2011-04-26
      • 2017-11-15
      • 2015-09-28
      相关资源
      最近更新 更多