VUE axios使用方法与跨域问题解决

简介: 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

前后端数据交互本地代理跨域问题,在本地localhost访问接口是要跨域的,浏览器的安全策略报错,需要在webpack配置proxyTable
config/index.js

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://www.centby.com',//设置你调用的接口域名和端口号 别忘了加http
        changeOrigin: true,//如果需要跨域
        pathRewrite: {
          '^/api': 'http://www.centby.com',//调用接口直接写‘/api/user/add’即可
        }
      }
    },

    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-


    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',

    /**
     * Source Maps
     */

    productionSourceMap: true,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}

使用方法示例:/api/v1/article/list/ = http://www.centby.com/v1/article/list/

      mounted(){
        //接口域名和端口用 api 代替
        this.articles()
      },
      methods: {
        articles (){
          let me = this;
          this.axios.post('/api/v1/article/list/', {
            token: '$13$iy6I3ab',
            page:1,
            pagesize:10,
            type:1
          }).then(function (res) {
            console.log(res);
            if(res.data.status == 1){
              console.log(res.data.data.data);
              //渲染页面
              me.list = res.data.data.data;
            }
          }).catch(function (error) {
            console.log(error);
          });

        }
      },

axios安装

npm install --save axios vue-axios

main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

用法

Vue.axios.get(api).then((response) => {
  console.log(response.data)
})

this.axios.get(api).then((response) => {
  console.log(response.data)
})

this.$http.get(api).then((response) => {
  console.log(response.data)
})
目录
相关文章
|
18天前
|
JSON 前端开发 JavaScript
3分钟让你学会axios在vue项目中的基本用法(建议收藏)
3分钟让你学会axios在vue项目中的基本用法(建议收藏)
47 0
|
2月前
|
XML JavaScript 前端开发
axios如何在vue中使用
axios如何在vue中使用
29 1
|
2月前
|
JavaScript
Vue3的v-model说明和使用方法
Vue3的v-model说明和使用方法
41 1
|
18天前
|
JavaScript API
Vue中axios拦截器怎么使用
Vue中axios拦截器怎么使用
|
1月前
|
Web App开发 前端开发 JavaScript
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
Spring Boot整合 mybatisplus(后端) Vue+echarts+Element UI+axios(前端)---前后端项目实例demo
27 1
|
1月前
|
前端开发 JavaScript Java
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
32 0
|
1月前
|
资源调度 JavaScript 前端开发
|
1月前
|
JavaScript
Vue中$root的使用方法
在 Vue 中,`$root`是一个属性,用于访问根组件实例。它的作用是连接所有其他的 Vue 实例组件,并向子组件提供全局配置和实例方法。根实例是 Vue 的上下文环境,包含了整个 Vue 应用的数据和方法。使用$root属性,可以方便地访问根实例的方法、数据和生命周期钩子函数。
QGS
|
3月前
|
JavaScript 安全 前端开发
手摸手vue2+Element-ui整合Axios
手摸手vue2+Element-ui整合Axios
QGS
29 0
|
3月前
Vue3 配置代理和使用全局axios请求数据
Vue3 配置代理和使用全局axios请求数据
81 1