使用 Axios 发送参数 - VueJS
Send Params With Axios - VueJS
所以我在 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 请求。
好的,所以我在 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));
?>
所以我在 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 请求。
好的,所以我在 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));
?>