axios现在以及是尤大大推荐使用的了,官方不在维护vue-reresource.

由于是地第一次使用axios, 在使用过程中猜了很大的坑

首先我们使用vue-cli创建的项目, 访问接口肯定是跨域了, 因为我们的本地服务默认的地址一般是localhost:8080 我们的服务器端肯定不是这个, 所以就形成跨域访问, axios不支持jsonp, 所以我们就要使用http-proxy-middleware中间件做代理,

http-proxy-middleware的github

安装

npm i axios --save-dev

npm install --save-dev http-proxy-middleware

// vue-cli 已经把http-proxy-middleware写在项目依赖里面了

引入axios

在项目的src/main.js引入axios

import axios from 'axios'

Vue.prototype.$axios = axios;
// axios 不支持Vue.use(axios)

配置http-proxy-middleware本地代理

打开config/index.js

var path = require('path')

module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './',
productionSourceMap: false,
// 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
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
修改这里修改这里修改这里
},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}

这里是默认的配置, 找到线面的dev对象里面的proxyTable修改

proxyTable: {
'/api': {
target:'http://www.baidu.com/api',
changeOrigin:true,
pathRewrite:{
'^/api': ''
}
}
}

target 的参数就是你要访问的服务器地址, 你在代码里面写/api就等于写了这个地址 , 比如我要访问http://www.baidu.com/api/login这个接口在代码里面只需写/api/login就可以了

至于build/dev.server.js 已经无需修改了, 里面已经有封装好了方法了

// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
var options = proxyTable[context]
if (typeof options === 'string') {
options = { target: options }
}
app.use(proxyMiddleware(options.filter || context, options))
})

网上好多的解决方案都是在build/dev.server.js里面自己在加内容, 完全不用了

做完上述操作之后一定要重启服务ctrl+c然后npm run dev

做完上述操作之后一定要重启服务ctrl+c然后npm run dev

做完上述操作之后一定要重启服务ctrl+c然后npm run dev

然后我们就可以用axios访问接口了

	this.$axios({
method: "POST",
withCredentials: false,
url: "/api/login",
data: {
name: "1511328705UZVQ",
psd: "123456"
}
})
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.log(err);
});

关于vue2.x使用axios以及http-proxy-middleware代理处理跨域的问题的更多相关文章

  1. 使用axios以及http-proxy-middleware代理处理跨域的问题

    axios现在以及是尤大大推荐使用的了,官方不在维护vue-reresource. 由于是地第一次使用axios, 在使用过程中猜了很大的坑 首先我们使用vue-cli创建的项目, 访问接口肯定是跨域 ...

  2. [vue] [axios] 设置代理实现跨域时的纠错

    # 第一次做前端工程 # 记一个今天犯傻调查的问题 -------------------------------------------------------------------------- ...

  3. vue中axios调用接口和用node.js跨域

    <script>const API_PROXY = 'https://bird.ioliu.cn/v1/?url='import axios from 'axios'export defa ...

  4. react添加多个域名proxy代理,跨域

    在package.json中加入如下: { "name": "demo", "version": "0.1.0", &q ...

  5. Angular:使用前端proxy代理解决跨域问题

    ①在项目文件的根目录下新建文件proxy.config.json { "/": { "target": "http://127.0.0.1:3000& ...

  6. vue 解决axios请求出现前端跨域问题

    vue 解决axios请求出现前端跨域问题 最近在写纯前端的vue项目的时候,碰到了axios请求本机的资源的时候,出现了访问报404的问题.这就让我很难受.查询了资料原来是跨域的问题. 在正常开发中 ...

  7. Vue用axios跨域访问数据

    Vue用axios跨域访问数据axios是vue-resource的替代品,vue-resource不再维护.安装axios:npm install axios使用vue-cli开发时,由于项目本身启 ...

  8. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十二 || 三种跨域方式比较,DTOs(数据传输对象)初探

    更新反馈 1.博友@落幕残情童鞋说到了,Nginx反向代理实现跨域,因为我目前还没有使用到,给忽略了,这次记录下,为下次补充.此坑已填 2.提示:跨域的姊妹篇——<三十三║ ⅖ 种方法实现完美跨 ...

  9. vue axios springBoot 跨域session丢失

    前端: 在引入axios的地方配置 axios.defaults.withCredentials=true,就可以允许跨域携带cookie信息了,这样每次发送ajax请求后,只要不关闭浏览器,得到的s ...

随机推荐

  1. 网络编程之TCP协议与UDP协议

    了解网络就要了解一些基本的协议今天主要跟大家分享一些关于TCP 协议UDP协议的相关知识 首先介绍一下TCP协议 TCP(Transmission Cintrol Protocol)可靠的.面向连接的 ...

  2. ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]锁定状态,无法插入数据

    PUT /twitter/_settings { "index.blocks.read_only_allow_delete": null } 官网给出的解决办法

  3. CycleGAN --- Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks

    文章地址:http://openaccess.thecvf.com/content_ICCV_2017/papers/Zhu_Unpaired_Image-To-Image_Translation_I ...

  4. ECMAScript 2015 可迭代协议:迭代普通对象

    可迭代协议允许 JavaScript 对象去定义或定制它们的迭代行为, 例如(定义)在一个 for..of结构中什么值可以被循环(得到). 一些内置类型都是内置的可迭代类型并且有默认的迭代行为( 比如 ...

  5. app自动化appium使用内置adb命令

    一.Appium-server使用 1.登陆页面 高级设置:可以设置Android 和 IOS 日志级别:dabug非常详尽的日志 记录python代码向他发送的请求以及他在收到请求后做的一系列处理 ...

  6. Python入门习题4.文本进度条

    例4.1.设置一组文本进度条,使之运行效果如下: --------执行开始--------% 0 [->**********]%10 [*->*********]%20 [**->* ...

  7. Android应用程序开发之图片操作(二)——工程图片资源的加载及OOM的处理

    (一)工程图片资源的加载方法 在Android应用程序开发之图片操作(一)中,详细说明了如何操作各种资源图片,只是有的没有附上示例代码,在此,我将针对项目工程中的图片资源的显示加载进行说明.官方说明, ...

  8. 71.Edit Distance(编辑距离)

    Level:   Hard 题目描述: Given two words word1 and word2, find the minimum number of operations required ...

  9. mysql数据库操作指令汇总

    1.mysql -u root -p 登录数据库 2.表结构相同两表数据对拷 insert into A select * from B(插入全部字段数据)   insert into A(字段1.字 ...

  10. 一些常见的js校验

    今天有时间来总结一下那些常用的js校验: vernull = function(value){//非空校验 if(value.trim(value).length == 0){ return fals ...