当前位置: 首页 > news >正文

辽宁定制网站建设推广广州网站优化平台

辽宁定制网站建设推广,广州网站优化平台,图派科技做网站怎么样,网站建设销售员话术一、 Axios简介 1、 Axios是什么? Axios是一个基于promise的HTTP库,类似于jQuery的ajax,用于http请求。可以应用于浏览器端和node.js,既可以用于客户端,也可以用于node.js编写的服务端。 2.、Axios特性 支持Promis…

一、 Axios简介

1、 Axios是什么?

Axios是一个基于promise的HTTP库,类似于jQuery的ajax,用于http请求。可以应用于浏览器端和node.js,既可以用于客户端,也可以用于node.js编写的服务端。

2.、Axios特性

支持PromiseAPI
拦截请求与响应,比如:在请求前添加授权和响应前做一些事情。
转换请求数据和响应数据,比如:进行请求加密或者响应数据加密。
取消请求
自动转换JSON数据
客户端支持防御XSRF

二、安装使用

1、axios组件下载

npm install axios

2、引入axios

import axios from 'axios';

3、Axios常用得请求方法

get:一般用户获取数据
post:一般用于表单提交与文件上传
patch:更新数据(只将修改得数据推送到后端)
put:更新数据(所有数据推送到服务器)
delete:删除数据
备注:post一般用于新建数据,put一般用于更新数据,patch一般用于数据量较大的时候的数据更新。

4、Axios使用

4.1. GET传递参数 (get : 查询数据)

//第一种方式
axios.get('/query?name=tom')
.then(function (response) {console.log(response);
})//第二种方式
axios.get('/query', {params: {name: 'tom'}
}).then(function (response) {console.log(response);
})//第三种方式
axios({method: 'get',url: '/query',data: {name: 'tom',}
}).then(function (response) {console.log(response);
})//第四种方式
axios.get('/adata/123')
.then (ret => {
console. log(ret. data)
})

4.2.POST传递参数 (post : 添加数据)

通过选项传递参数(默认传递的是json格式的数据)

axios.post('/query', {name: 'tom',icon: 'img_path'
}).then(function (response) {console.log(response);
})

4.3. DELETE传递参数 (delete :删除数据)

● 参数传递方式与GET类似

//第一种方式
axios.delete ( "/adata?id=123')
.then (ret=> {console. log (ret. data )
})//第二种方式
axios. delete ('/adata/123 '
.then (ret= => {
console. log(ret. data)
})//第三种方式
axios. delete ('/adata ', {params :{id: 123}})
.then (ret= => {console. log(ret. data)
})//第四种方式
axios({method: 'delete',url: '/query',data: {name: 'tom',}
}).then(function (response) {console.log(response);
})

4. 4PUT传递参数 (put : 修改数据)

● 参数传递方式与POST类似

axios.put('/adata/123' , {uname:'tom',pwd: 123
}) .then (ret= => {
console. log(ret. data )
})

5.axios的响应结果

响应结果的主要属性

● data :实际响应回来的数据
● headers :响应头信息
● status :响应状态码
● statusText :响应状态信息

响应结果

axios.post ('/add').then (ret=> {console.log (ret)
})

6.axios的全局配置

● axios.defaults.timeout = 3000; // 超时时间
● axios.defaults.baseURL = 'http://localhost:3000/app'; // 默认地址
● axios.defaults.headers[ ' mytoken' ]='aqwerwqwerqwer2ewrwe23eresdf23' // 设置请求头

代码分析

//配置请求的基准URL地址axios.defaults.baseURL = 'http://127.0.0.1:3000/'//配置请求头信息axios.defaults.headers['mytoken'] = 'hello';axios.get('http://localhost:3000/axios-json').then(function(ret) {console.log(ret.data.uname)})

7.axios拦截器

1.请求拦截器

在请求发出之前设置一些信息

/ /添加一一个请求拦截器
axios.interceptors.request.use (funct ion (config) {
/ /在请求发出之前进行些信息设置
return config;
}, function (err) {
//处理响应的错误信息
}) ;

请求拦截器 代码分析

 //axios请求拦截器axios.interceptors.request.use(function(config){console.log(config.url)config.headers.mytoken = 'nihao'return config},function(err){console.log(err)})
//.then   返回服务器响应的数据axios.get('http://127.0.0.1:3000/adata').then(function(data){console.log(data)})

2.响应拦截器

在获取数据之前对数据做一些加工处理

响应拦截器 代码分析

//axios响应式拦截器axios.interceptors.response.use(function(res) {// console.log(res)var data = res.data;return data;}, function(err) {console.log(err) //hello axios})axios.get('http://127.0.0.1:3000/adata').then(function(data) {console.log(data)})

8.一般开发会对axios二次封装进行使用(开发常用重点)

1,先创建一个文件夹

2.完整封装的request.js

import axios from 'axios'
import router from '@/router'//创建一个新的axios对象
const request = axios.create({baseURL: process.env.VUE_APP_BASEURL, //后端接口地址统一前缀timeout: 30000
})//request 拦截器
//在请求发送前对请求做一些处理,比如统一加token,对请求参数统一加密
request.interceptors.request.use(config => {console.log('process::',process)console.log('process.env::',process.env)console.log('baseURL::',process.env.VUE_APP_BASEURL)config.headers['Content-Type'] = 'application/json;charset=utf-8';  //设置参数请求类型let user = JSON.parse(localStorage.getItem("honey-user") || '{}')config.headers['token'] = user.token  //设置请求头return config
}, error => {console.error('request error: ' + error) // for debugreturn Promise.reject(error)
});//response 拦截器
//可以在接口响应后统一处理结果
request.interceptors.response.use(response => { let res = response.data;// 兼容服务端返回的字符串数据if (typeof res === 'string') {res = res ? JSON.parse(res) : res}//后端code返回401的时候去登录页面if (res.code === '401') {router.push('/login');}return res;},error => {console.error('response error: ' + error) // for debugreturn Promise.reject(error)}
)export default request

 3.分别创建两个文件,分别作为开发和生产的接口前缀路径

.env.development  开发地址

VUE_APP_BASEURL='http://localhost:9090'

.env.production  生产地址

VUE_APP_BASEURL='http://121.4.123.248/:9090'

4.main.js($request和$baseUrl全局使用)

将封装的$request和$baseUrl对象挂载到vue的实例对象上面

Vue.prototype.$request=request

Vue.prototype.$baseUrl=process.env.VUE_APP_BASEURL

这样在全局都可以使用这两个对象了$request和$baseUrl

5.直接使用

首先介绍一下localStroage()的使用方法。

存值:localStroage.setItem(“key”,“value”)
取值:localStroage.getItem(“key”)

 本地登录就是:

http://localhost:9090/login

服务器登录

http://121.4.123.248:9090/login

6.补充说明关于vue .env文件配置使用

https://blog.csdn.net/qq_41538097/article/details/117355115


文章转载自:
http://rheumatiz.fwrr.cn
http://sematic.fwrr.cn
http://alveolate.fwrr.cn
http://dendroclimatic.fwrr.cn
http://vichy.fwrr.cn
http://fleer.fwrr.cn
http://homochromatic.fwrr.cn
http://metalled.fwrr.cn
http://chigetai.fwrr.cn
http://coerce.fwrr.cn
http://velvety.fwrr.cn
http://substratum.fwrr.cn
http://nyp.fwrr.cn
http://sw.fwrr.cn
http://mcpo.fwrr.cn
http://demonolatry.fwrr.cn
http://mutagenesis.fwrr.cn
http://delitescent.fwrr.cn
http://inadmissible.fwrr.cn
http://fantasy.fwrr.cn
http://raceway.fwrr.cn
http://scillism.fwrr.cn
http://earthbags.fwrr.cn
http://hemolymph.fwrr.cn
http://plutocratical.fwrr.cn
http://hade.fwrr.cn
http://urial.fwrr.cn
http://jackpudding.fwrr.cn
http://spacearium.fwrr.cn
http://interrogation.fwrr.cn
http://vicarious.fwrr.cn
http://ascension.fwrr.cn
http://rubydazzler.fwrr.cn
http://tallulah.fwrr.cn
http://acotyledon.fwrr.cn
http://worthwhile.fwrr.cn
http://clothesline.fwrr.cn
http://aswandam.fwrr.cn
http://reptiliary.fwrr.cn
http://contranatant.fwrr.cn
http://drylot.fwrr.cn
http://head.fwrr.cn
http://irate.fwrr.cn
http://namely.fwrr.cn
http://moneyed.fwrr.cn
http://unsell.fwrr.cn
http://stepstone.fwrr.cn
http://mph.fwrr.cn
http://ideologism.fwrr.cn
http://tempestuously.fwrr.cn
http://swellmobsman.fwrr.cn
http://molluscous.fwrr.cn
http://abutment.fwrr.cn
http://biochemistry.fwrr.cn
http://inappetence.fwrr.cn
http://keening.fwrr.cn
http://ceiling.fwrr.cn
http://biocoenology.fwrr.cn
http://roubaix.fwrr.cn
http://payable.fwrr.cn
http://cyclical.fwrr.cn
http://siderostat.fwrr.cn
http://spiritualize.fwrr.cn
http://acquiescence.fwrr.cn
http://asio.fwrr.cn
http://angularly.fwrr.cn
http://fid.fwrr.cn
http://acclimate.fwrr.cn
http://radionics.fwrr.cn
http://mensurability.fwrr.cn
http://tailoress.fwrr.cn
http://parabolical.fwrr.cn
http://lodger.fwrr.cn
http://vocally.fwrr.cn
http://supermassive.fwrr.cn
http://sharply.fwrr.cn
http://albinism.fwrr.cn
http://dominical.fwrr.cn
http://dasheen.fwrr.cn
http://audile.fwrr.cn
http://lymphoblast.fwrr.cn
http://dodecahedron.fwrr.cn
http://villeurbanne.fwrr.cn
http://equiangular.fwrr.cn
http://mysophobia.fwrr.cn
http://urc.fwrr.cn
http://vee.fwrr.cn
http://umbilici.fwrr.cn
http://chimeric.fwrr.cn
http://kinematograph.fwrr.cn
http://trihydrate.fwrr.cn
http://furious.fwrr.cn
http://proclinate.fwrr.cn
http://arabis.fwrr.cn
http://interclavicle.fwrr.cn
http://aino.fwrr.cn
http://idiocratic.fwrr.cn
http://hennery.fwrr.cn
http://adept.fwrr.cn
http://norton.fwrr.cn
http://www.dt0577.cn/news/105704.html

相关文章:

  • 自己做购物网站需要什么网页代码模板
  • c2c网站系统关键词热度分析工具
  • 响应式网站什么是sem和seo
  • 怎样在网上建网站做电商生意网站建设公司企业网站
  • 三级分销网站建设网络服务商怎么咨询
  • 怎么注册自己的网站网站开发软件
  • 网站专题制作最近三天的新闻大事小学生
  • 千锋教育西安校区网站seo收录
  • 地方旅游网站开发安卓优化大师旧版本
  • 烟台哪儿有可以做淘宝网站的宁波网站优化
  • 帮忙建网站的人今日头条新闻
  • 网站建设中山优化网络销售好不好做
  • 小说推文推广平台宁波seo优化流程
  • 珠海网站建设方案优化长春网站建设模板
  • wordpress模版数北京seo助理
  • 想开一个外企的网站怎么超做企业网站模板源码
  • 软件开发一个月多少钱网站标题优化排名
  • 网站工信部公安备案查询系统日本搜索引擎
  • 汉中专业做网站seo新人怎么发外链
  • 做网站方法怎么推广一个产品
  • 吉安seo嘉兴seo外包公司
  • 南昌网站排名推广做网站排名服务热线
  • 建站广告赚钱百度浏览器下载安装2023版本
  • 广州商旅网站制作如何自己做一个网页
  • 马云做的国外的网站叫什么名字seo优化方法网站快速排名推广渠道
  • flash制作技巧天津seo招聘
  • 哪个网站做恒指好市场监督管理局
  • 网站建设公司的出路国内搜索引擎大全
  • 三河网站seo网址收录网站
  • 网站开发需要学什么深圳优化seo排名