我正在写代码从MySQL数据库中获取数据,并使用Vuejs,
this.$http.get("api.php?action=read")
.then(response => {
console.log(response);
}, error => {
console.log(error);
});
此请求返回所有api.php内容,但不返回数据。
这是api.php文件
<?php
require_once('include/config.php');
$res = (['error' => false]);
$action = 'read';
if (isset($_GET['action'])) {
$action = $_GET['action'];
}
if($action == 'read') {
$images = array();
$query = 'SELECT * FROM images ORDER BY id DESC';
$select_images = mysqli_query($connection, $query);
while($image = mysqli_fetch_assoc($select_images)) {
array_push($images, $image);
}
echo json_encode($images);
}
mysqli_close($connection);
header("Content-type: application/json");
echo json_encode($images);
die();
?>
有人能帮帮我吗?
发布于 2018-07-18 10:34:26
它起作用了,
我添加了
header("Access-Control-Allow-Origin: *");
添加到API文件
并从localhost/vuejs链接发出get请求,而不是像localhost:8080那样工作
谢谢
发布于 2018-11-08 05:29:52
在php中包含头文件。注意:在你返回任何东西之前,应该是第一行。
<?php
header("Access-Control-Allow-Origin: *");
并尝试使用命令提示符运行php code prompt:
php -S localhost:8888 ajaxfile.php
注意:尝试使用不同的端口号,因为我无法从运行vueProject的相同8080端口获取api。这将使用您的php创建一个api端点。它将监听http://localhost:8888/projectname/api.php。
现在,当使用axios.get(http://localhost:8888/projectname/api.php)时,会得到api.I的响应。我还没有尝试过不使用axios模块,但我猜它应该可以工作。
allRecords: function(){
axios.get('http://localhost:8888/VueAxios/ajaxfile.php').then(response => {
this.users = response.data
console.log(response.data)
})
.catch(function (error) {
console.log(error);
});
}
这个函数对我很有效。注意: axios在这种情况下是非常方便的。希望能有所帮助。
发布于 2018-07-18 08:57:57
尝试使用response.body
获取响应数据:
this.$http.get("api.php?action=read")
.then(response => {
console.log(response.body);
}, error => {
console.log(error);
});
https://stackoverflow.com/questions/51391969
复制相似问题