1, 安装

cnpm install axios --save

2, 在main.js中引入

import Axios from 'axios'
// 挂在在Vue上
Vue.prototype.$axios=Axios;

3, get请求:

<script>
export default {
name: 'HelloWorld',
data() {
return {
newsData: []
}
},
// 组件创建时执行
created() {
// 直接参数方式
this.$axios("http://www.wwtliu.com/sxtstu/news/juhenews.php?type=junshi&count=30")
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err)
}); // 间接参数方式:
this.$axios("http://www.wwtliu.com/sxtstu/news/juhenews.php", {
params: {
type: 'junshi',
count:
}
}).then(res => {
this.newsData = res.data.result.data
console.log(this.newsData)
}).catch(err => {
console.log(err)
}) }
}
</script>

页面渲染

<div class="hello">

    <div>
<ul>
<div>
<li v-for="news in newsData">
<img :src="news.thumbnail_pic_s" alt="">
<p> {{ news.title }}</p>
</li>
</div>
</ul>
</div> </div>

4, post 请求:

1), axios 接受的post请求参数的格式 是 form-data 格式

?name=iwen&passwd=1234

2), x-from-urlencoded:

{name: "iwen", passwd: "abc"}

使用第三方库 qs 进行转换

<script>

  import qs from "qs"

  export default {
name: "PostRequest",
data() {
return {
postList: []
}
},
create() {
// 引入第三方库, 转变 x-form-urlencoded 格式 为 form 格式
this.$axios.post("www.wwtliu.com/sxtstu/blueberrypai/login.php", qs.stringify({
user_id: "wenbronk",
password: ""
})
).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
}
}
</script>

5, 全局的 axios的默认值

在main.js中进行全局配置

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
// 引入
import Axios from 'axios' Vue.config.productionTip = false // 挂在在Vue上
Vue.prototype.$axios=Axios; // 在之后不需要 写 网址, uri了
Axios.defaults.baseURL = 'https://api.example.com';
// https 认证
Axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// 设置heads, 可以不需要qs设置
Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; /* eslint-disable no-new */
new Vue({
el: '#app',
components: { App },
template: '<App/>'
})

6, 拦截器:

在 thne 或者 catch 之前, 做拦截处理操作

在main.js中进行拦截

// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
}); // 添加响应拦截器
Axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});

进行参数校验或者 后端response校验

7, 跨域解决方案:

在config的 index.js 中添加:

    // 处理跨域
proxyTable: {
"/api": {
target: "http://localhost:8080",
changeOrigin: true,
pathRewrite: {
"^/api": ""
}
}
},

然后在main.js中添加host 代理

import Vue from 'vue'
import App from './App'
// 引入
import Axios from 'axios' Vue.config.productionTip = false // 挂在在Vue上
Vue.prototype.$axios=Axios; // 解决跨域:
Vue.prototype.HOST = '/api'

然后 在 post请求中的地址, 改写为

 this.HOST + “/login.html”

但只能在测试阶段使用, 正式环境中需要后端解决 !!!!

post 请求可以使用请求拦截器的方式进行拦截

import router from './router'
import Axios from 'axios'
import qs from "qs" Vue.config.productionTip = false // 添加 axios
Vue.prototype.$axios = Axios
Axios.defaults.baseURL = 'https://www.wwtliu.com';
Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; // 添加 post 请求拦截器
Axios.interceptors.request.use(function (config) {
if (config.method == "post") {
config.data = qs.stringify(config.data)
}
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
}); // 添加响应拦截器
Axios.interceptors.response.use(function (response) { return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});

springboot 解决跨域方案:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport { @Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// 设置你要允许的网站域名,如果全允许则设为 *
config.addAllowedOrigin("*");
// 如果要限制 HEADER 或 METHOD 请自行更改
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
// 这个顺序很重要哦,为避免麻烦请设置在最前
bean.setOrder();
return bean;
} }

前端:

import Axios from 'axios'
// Vue.prototype.$axios=Axios;
// 在之后不需要 写 网址, uri了
Vue.prototype.baseURL = 'http://localhost:8000/';
// Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; const axiosIns = Axios.create({
baseURL: Vue.prototype.baseURL,
timeout: ,
withCredentials: true, // 跨域
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Access-Control-Allow-Origin": "*",
}
});
Vue.prototype.$axios=axiosIns;

vue-08-axios-get-post-跨域的更多相关文章

  1. vue下axios和fetch跨域请求

    1.在config的index.js下面进行常用跨域配置代码:proxyTable: { '/apis': { //使用"/api"来代替"http://xxxx.cn& ...

  2. Vue中axios访问 后端跨域问题

    public class AllowOriginFilter implements Filter { @SuppressWarnings("unused") public void ...

  3. 解决Django+Vue前后端分离的跨域问题及关闭csrf验证

      前后端分离难免要接触到跨域问题,跨域的相关知识请参:跨域问题,解决之道   在Django和Vue前后端分离的时候也会遇到跨域的问题,因为刚刚接触Django还不太了解,今天花了好长的时间,查阅了 ...

  4. nginx处理vue打包文件后的跨域问题

    起因 在vue文件打包后,项目脱离了vue配置的反向代理配置,还是会报跨域的错误,或者直接打不开本地文件, 但是此刻我们想打开打包后的文件,测试一下文件有没有错误,因为经常会存在开发阶段没有问题,打包 ...

  5. VUE系列三:实现跨域请求(fetch/axios/proxytable)

    1. 在 config/index.js 配置文件中配置proxyTable 'use strict' // Template version: 1.3.1 // see http://vuejs-t ...

  6. vue 开发和生产的跨域问题

    开发阶段 在开发环境与后端调试的时候难免会遇到跨域问题,在 vue 项目中常用的是 proxyTable,这个用起来很方便. 打开 config 文件夹下面的 index.js,找到 dev 开发模式 ...

  7. Vue+webpack项目中实现跨域的http请求

    目前Vue项目中对json数据的请求一般使用两个插件vue-resource和axios, 但vue-resource已经不再维护, 而axios是官方推荐的且npm下载量已经170多万,github ...

  8. vue配置二级目录&vue-axios跨域办法&谷歌浏览器设置跨域

    一.根据官方建议,dist打包的项目文件放在服务器根目录下,但是很多时候,我们并不能这样做,当涉及到二级目录设置多层深埋的时候,就需要在webpack配置文件里去设置一下了. 在webpack.con ...

  9. vue反向代理(解决跨域)

    1,vue中有提供反向代理的接口,就是config/index.js中的proxyTable,我的脚手架版本是2.9.6,proxyTable配置初始为空,如下图. 2,将proxyTable配置如下 ...

  10. Vue ---- ajax cookies 插件安装 跨域问题 element-ui bootscript 导入

    目录 补充: 流式布局样式 Django国际化配置 Django的TODO注释 Vue的ajax插件:axios 安装 配置 如何使用? Vue的cookies插件:cookies 安装: main. ...

随机推荐

  1. openmp入门总结

    Ref: https://wdxtub.com/2016/03/20/openmp-guide/ 简介 这门课作为 ECE 中少有的跟计算机科学相关的课,自然是必上不可.不过无论是 OpenMP 还是 ...

  2. vue el-upload form 同时提交

    项目需要form 表单和文件上传同时在一个请求,废话不多说上代码: 上传的组件使用pug格式 .row.my-4 .col-12 el-form(:model='domain', :rules='va ...

  3. 关于H5在微信获取授权

    很尴尬,flag倒了很久,这才来更新. 1.作为一枚小前端,所做的就是把微信获取授权之后的链接和所需的参数给到后端,定好之后只要获取链接就好了.(⊙o⊙)…确实就是这么简单,基本上这种授权是需要后端来 ...

  4. kubernetes1.7.6 ha高可用部署

    写在前面:  1. 该文章部署方式为二进制部署. 2. 版本信息 k8s 1.7.6,etcd 3.2.9 3. 高可用部分 etcd做高可用集群.kube-apiserver 为无状态服务使用hap ...

  5. JSP与Servlet的关系

    之前的文章里有说过,其实Servlet比JSP出现的早,JSP的本质就是Servlet,但是一个是jsp页面,一个是java类,硬要说他们的本质是一样的,搁谁都有点难以置信.后面的结论会更加让你吃惊. ...

  6. ppt制作动态表格与文字

          在工作中经常与ppt打交道的小伙伴们,是不是非常想让自己做的ppt图表能够动起来,显得高大上一点呢?比如让柱形图慢慢长起来,让折线图慢慢画出来,让文字像字幕一样缓缓上升?本文将给大家整理几 ...

  7. Minimum setup for Apache+AD SSO

    参照: http://www.grolmsnet.de/kerbtut/ https://docs.typo3.org/typo3cms/extensions/ig_ldap_sso_auth/2.1 ...

  8. 关于使用jquery.cookie.js存cookie中文出现乱码问题

    一.在Web开发中,有事为了页面之间传值,我们会用到cookie.但是当在cookie中存值为中文汉字时就会出现乱码! 这是一个简单例子: <!DOCTYPE html><html ...

  9. 《python语言程序设计》_第4章_选择

    第四章 # 4.1 引言 布尔表达式:选择语句选择的条件. 程序: import math #加载math模块radius=eval(input("Enter an integer:&quo ...

  10. 华为SSH认证配置

    [设备型号]华为switch-S5700s ————1: [SW1] rsa/dsa local-key-pair create #创建本地密钥对 The key name will be: SW1_ ...