4.vue引入axios同源跨域
前言:
跨域方案有很多种,既然我们用到了Vue,那么就使用vue提供的跨域方案。
解决方案:
1.修改HttpRequestUtil.js
import axios from 'axios' export var baseurl = '/api'
/**
* Get请求
*/
export function get(url, callback){
console.log('测试get请求')
axios.get(baseurl+url)
.then(function (response) {
console.log(response)
callback(response.data,true)
})
.catch(function (error) {
console.log(error)
callback(null,false)
})
} export default {
get
}
2.修改index.js
'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation. const path = require('path') module.exports = {
dev: { // Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'http://127.0.0.1:8088',//设置你调用的接口域名和端口号 别忘了加http
changeOrigin: true,
pathRewrite: {
'^/api': 'http://127.0.0.1:8088'//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
}
}
}, // Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8090, // 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 of
bundleAnalyzerReport: process.env.npm_config_report
}
}
proxyTable: {
'/api': {
target: 'http://127.0.0.1:8088',//设置你调用的接口域名和端口号 别忘了加http
changeOrigin: true,
pathRewrite: {
'^/api': 'http://127.0.0.1:8088'//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
}
}
},
|
4.vue引入axios同源跨域的更多相关文章
- vue-cli 引入axios及跨域使用
使用 cnpm 安装 axios cnpm install axios --save-dev 安装其他插件的时候,可以直接在 main.js 中引入并 Vue.use(),但是 axios 并不能 u ...
- .net core3.1 webapi + vue.js + axios实现跨域
我所要讲述的是,基于.net core3.1环境下的webapi项目,如何去使用axios对接前端的vue项目 既然谈到axios,这里贴出axios的官方文档地址: http://www.axios ...
- vue 使用axios 出现跨域请求的两种解决方法
最近在使用vue axios发送请求,结果出现跨域问题,网上查了好多,发现有好几种结局方案. 1:服务器端设置跨域 header(“Access-Control-Allow-Origin:*”); h ...
- 记录:使用springboot的cors和vue的axios进行跨域
一.编写一个配置类,并且注册CorsFilter: 注意允许跨域的域名不要写错 @Configuration public class ZysuyuanCorsConfiguration { @Bea ...
- Vue(项目踩坑)_解决vue中axios请求跨域的问题
一.前言 今天在做项目的时候发现axios不能请求跨域接口 二.主要内容 1.之前直接用get方式请求聚合数据里的接口报错如下 2.当前请求的代码 3.解决方法 (1)在项目目录中依次找到:confi ...
- vue.js axios实现跨域http请求接口
跨域post实例,用到了qs组件来避开ajax信使请求,并兼容Android. import axios from 'axios'; import qs from 'qs'; axios.post(' ...
- vue.js学习之 跨域请求代理与axios传参
vue.js学习之 跨域请求代理与axios传参 一:跨域请求代理 1:打开config/index.js module.exports{ dev: { } } 在这里面找到proxyTable{}, ...
- 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库
一.axios Vue更新到2.0之后宣告不再对vue-resource更新,推荐使用axios,axios是一个用于客户端与服务器通信的组件,axios 是一个基于Promise 用于浏览器和 no ...
- 前端vue开发中的跨域问题解决,以及nginx上线部署。(vue devServer与nginx)
前言 最近做的一个项目中使用了vue+springboot的前后端分离模式 在前端开发的的时候,使用vue cli3的devServer来解决跨域问题 上线部署则是用的nginx反向代理至后台服务所开 ...
随机推荐
- android Window(一)从setConetView说起
Activity的源码 首先从setContentView这里调用的mWindow的 setConetView() private Window mWindow; public void setCon ...
- 为什么我用gets不行呢?系统无视了我的存在!!!
梗概:为什么我用gets不行呢?系统无视了我的存在!!!我还没输入东东啊..怎么就提示[请安任意键继续]的?? 原来是缓冲区的问题啊? 一.什么是缓冲区 缓冲区又称为缓存,它是内存空间的一部分.也就是 ...
- Unity Animation动画倒播
- (转)python 集合,列表,元组,字符串,文件等操作总结
原文:http://www.cnblogs.com/songqingbo/tag/python%E5%87%BD%E6%95%B0/
- Android多线程源码学习笔记一:handler、looper、message、messageQueue
最近在学习Android多线程相关知识的源码,现在把自己的笔记整理一下,写出来加深印象. Android多线程通讯的核心是handler.looper.message.messageQueue,这篇文 ...
- Quartz使用(2) - Quartz核心接口Scheduler、Job
quartz的核心接口如下: 接口 含义 Scheduler scheduler的主要API接口 Job 任务实现接口,期望调度器能够执行 JobDetail 用于定义Job实例 Trigger 调度 ...
- for循环笔记
JS获取元素方法——ById和ByTagName方法的区别 1.通过id获取,前面就只能是document,不能是其他的,但是ByTagName前面可以是document,也可以跟一个别的元素 #li ...
- Powershell(3)
Powershell 可以使用powershell管理的服务 share point, exchange, lync, windows azure, window server, system cen ...
- eclipse编码转换
一般Java文件编码格式是UTF-8的.以下以默认GBK改为UTF-8为例. 1.改变整个工作空间的编码格式,这样以后新建的文件也是新设置的编码格式. eclipse->window-& ...
- node模块机制
一.node模块化机制 1.commonJS模块规范包括三部分:模块引用.模块定义.模块标识.例如: //math.js exports.add = function(){ var sum = ...