一、安装 axios

npm install --save axios

二、安装好后,在需要请求接口的页面:

<script>
import axios from "axios";
export default {
  mounted() {
    axios.get("http://lanyue.ink:8123/api/moments").then(res => {
      console.log(res.data);
    });
  }
};
</script>

如果控制台报错:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ...

这是跨域访问的问题,需要设置后端接口:

1、打开laravel后端项目,安装 barryvdh/laravel-cors

composer require barryvdh/laravel-cors

2、在app/Http/Kernel.php 的 protected $middleware 中添加

\Barryvdh\Cors\HandleCors::class

 结果:

 

Logo

前往低代码交流专区

更多推荐