【发布时间】: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! :)