当前位置:   article > 正文

axios解决跨域问题(vue-cli3.0)

vue 3.0 axios跨域

axios解决跨域问题(vue-cli3.0)


一、什么是跨域

1、跨域

  指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。

2、同源策略

  是指协议,域名,端口都要相同,其中有一个不同都会产生跨域,在请求数据时,浏览器会在控制台中报一个异常,提示拒绝访问。

3、跨域问题怎么出现的

  开发一些前后端分离的项目,比如使用 SpringBoot + Vue 开发时,后台代码在一台服务器上启动,前台代码在另外一台电脑上启动,此时就会出现问题。
  比如:

    后台 地址为 http://192.168.70.77:8081
    前台 地址为 http://192.168.70.88:8080
  此时 ip 与 端口号不一致, 不符合同源策略,造成跨域问题。


二、使用 axios 演示并解决跨域问题(vue-cli3.0)

1、项目创建、与 axios 的使用

(1)step1:创建 vue 项目
  参考 https://www.cnblogs.com/l-y-h/p/11241503.html

(2)step2:使用 axios

  参考 https://www.cnblogs.com/l-y-h/p/11656129.html

2、跨域问题重现

(1)step1:删去 vue 项目初始提供的部分代码,如下图

运行截图:

(2)step2:使用 axios

  1. 【App.vue】
  2. <template>
  3. <div>
  4. <button @click="testAxios">TestAxios</button>
  5. </div>
  6. <!--App -->
  7. </template>
  8. <script>
  9. // 引入axios
  10. import Axios from 'axios'
  11. export default {
  12. methods: {
  13. testAxios() {
  14. const url = 'https://www.baidu.com/'
  15. Axios.get(url).then(response => {
  16. if (response.data) {
  17. console.log(response.data)
  18. }
  19. }).catch(err => {
  20. alert('请求失败')
  21. })
  22. }
  23. }
  24. }
  25. </script>
  26. <style>
  27. </style>

此时点击按钮,会出现跨域问题。

(3)常见错误解决

  1. 【question1:】
  2. 'err' is defined but never used (no-unused-vars)
  3. 这个问题,是由于 vue 项目安装了 ESLint 。
  4. 暴力解决:直接关闭 ESLint
  5. package.json 文件中 添加
  6. "rules": {
  7. "generator-star-spacing": "off",
  8. "no-tabs":"off",
  9. "no-unused-vars":"off",
  10. "no-console":"off",
  11. "no-irregular-whitespace":"off",
  12. "no-debugger": "off"
  13. }

3、解决跨域问题

(1)step1:配置 baseURL
  可以自定义一个 js 文件,也可以直接在 main.js 中写。

  1. 【main.js】
  2. import Vue from 'vue'
  3. import App from './App.vue'
  4. // step1:引入 axios
  5. import Axios from 'axios'
  6. Vue.config.productionTip = false
  7. // step2:把axios挂载到vue的原型中,在vue中每个组件都可以使用axios发送请求,
  8. // 不需要每次都 import一下 axios了,直接使用 $axios 即可
  9. Vue.prototype.$axios = Axios
  10. // step3:使每次请求都会带一个 /api 前缀
  11. Axios.defaults.baseURL = '/api'
  12. new Vue({
  13. render: h => h(App),
  14. }).$mount('#app')

(2)step2:修改配置文件(修改后要重启服务)
  vue 3.0 通过 vue.config.js 文件 修改配置(若没有,则直接在项目路径下新建即可)。

  1. 【vue.config.js】
  2. module.exports = {
  3. devServer: {
  4. proxy: {
  5. '/api': {
  6. // 此处的写法,目的是为了 将 /api 替换成 https://www.baidu.com/
  7. target: 'https://www.baidu.com/',
  8. // 允许跨域
  9. changeOrigin: true,
  10. ws: true,
  11. pathRewrite: {
  12. '^/api': ''
  13. }
  14. }
  15. }
  16. }
  17. }

(3)step3:修改 axios 使用方式

  1. 【App.vue】
  2. <template>
  3. <div>
  4. <button @click="testAxios">TestAxios</button>
  5. </div>
  6. <!--App -->
  7. </template>
  8. <script>
  9. export default {
  10. methods: {
  11. testAxios() {
  12. // 由于 main.js 里全局定义的 axios,此处直接使用 $axios 即可。
  13. // 由于 main.js 里定义了每个请求前缀,此处的 / 即为 /api/
  14. // 经过 vue.config.js 配置文件的代理设置,会自动转为 https://www.baidu.com/,从而解决跨域问题
  15. this.$axios.get('/').then(response => {
  16. if (response.data) {
  17. console.log(response.data)
  18. }
  19. }).catch(err => {
  20. alert('请求失败')
  21. })
  22. }
  23. }
  24. }
  25. </script>
  26. <style>
  27. </style>

重启服务后,点击按钮,可以成功访问。

原文地址:https://www.cnblogs.com/l-y-h/p/11815452.html
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号