【问题标题】:Send Params With Axios - VueJS使用 Axios 发送参数 - VueJS
【发布时间】:2018-02-15 09:14:36
【问题描述】:

所以我正在使用 VueJS 中的 Axios 来发送 ajax 请求。但是,我在尝试发送请求时遇到了一些问题。

这是我的 JS 代码:

axios.post('http://localhost/app/php/search.php', {
    query: this.search //this should send the 'query' param
}).then(response => {
    this.variable = response.data
}).catch(e => {
    this.errors.push(e)
})

这是search.php文件:

<?php

    require '../Functions.php';
    $obj = new Functions();

    $array = $obj->search($_POST['query']);

    echo json_encode(array_values($array));
?>

我在 PHP 端收到以下错误:Notice: Undefined index: query in /path/to/app/search.php on line 6

为什么会发生这种情况?非常感谢任何帮助。

更新

this.search 是我的data 对象中的一个变量:

data () {
    return {
      search: ''
    }
  }

此变量绑定到文本字段:

<input type="text" v-model="search" @keyup.enter="search()" />

这在我的search() 方法中是我的 axios 请求。

【问题讨论】:

  • 不确定 axios/vuejs,但看看这是否有帮助:stackoverflow.com/questions/13000386/…。也许是类似的东西。与角度相同的东西:stackoverflow.com/questions/15485354/…
  • 帖子在哪里执行,this.search 是什么时候?
  • 我更新了@Bert 的帖子。
  • 好吧,看起来您将向 search.php 发送一个类似于 {query: "whatever search is"} 的对象。所以从客户端看来是对的?
  • 是的,我现在开始工作了。无论如何谢谢@Bert! :)

标签: php vue.js axios


【解决方案1】:

好的,所以我在 Angular 中查看了类似的问题,结果发现 Axios 正在发送 query 参数。但是,在 php 文件中,我必须使用来自 Axios 的 JSON 填充 POST 变量。这就是我的工作方式:

<?php

    require '../Functions.php';
    $obj = new Functions();

    //Populate POST variable with incoming JSON from Axios.
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)):
        $_POST = (array) json_decode(file_get_contents('php://input'), true);
    endif;

    $array = $obj->search($_POST['query']);

    echo json_encode(array_values($array));
?>

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多