Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
2017-11-27 00:00
2076 查看
摘要: 努力是一个漫长的过程......
https://www.kancloud.cn/yunye/axios/234845
PS: 使用之前引入axios.js
axios.all(iterable)
axios.spread(callback)
(2)axios(url[,config])
axios.request(config)
axios.get(url[,config])
axios.delete(url[,config])
axios.head(url[,config])
axios.post(url[,config])
axios.post(url[,data[,config]])
axios.put(url[,data[,config]])
axios.patch(url[,data[,config]])
axios.create([config])
https://www.kancloud.cn/yunye/axios/234845
PS: 使用之前引入axios.js
1.Axios是一个基于promise的HTTP库,可以用在浏览器和node.js中
2.执行GET请求
axios.get('url').then(function(response){ console.log(response.data) }).catch(function(error){ console.log(error.response) })
3.执行POST请求
axios.post('url',{data数据}).then(function(response){ console.log(response.data) }).catch(function(error){ console.log(error.response) })
4.执行多个并发请求
处理并发请求的助手函数axios.all(iterable)
axios.spread(callback)
function getUserAccount(){ return axios.get('url1') } function getUserPermissions(){ return axios.get('url2') } axios.all([getUserAccount(), getUserPermissions()]).then(axios.spread(function(acct, perm){ // 两个请求现在都执行完成 }))
5.axios API
(1)axios(config)(2)axios(url[,config])
6.请求方法的别名
在使用别名方法时,url/method/data这些属性都不必在配置中指定axios.request(config)
axios.get(url[,config])
axios.delete(url[,config])
axios.head(url[,config])
axios.post(url[,config])
axios.post(url[,data[,config]])
axios.put(url[,data[,config]])
axios.patch(url[,data[,config]])
7.创建实例
使用自定义配置创建一个axios实例axios.create([config])
8.实例方法
6中的方法都可以被实例使用9.请求配置
{ url: '', // 必需 method: '' baseURL: '' // 对data进行任何转换处理 transformRequest: [function(data){ }], // 对data进行任何转换处理 transformResponse: [function(data){ }], // 即将被发送的自定义请求头 headers: { }, // 即将与请求一起发送的URL参数 params: { }, // 序列号params paramsSerializer: function(params){ }, // 请求主体被发送的数据 data: { }, // 请求超时时间 timeout:1000, // 跨域请求是否需要使用凭证 withCredentials: false, // 允许自定义处理请求 adapter: function(){ }, // 应该使用HTTP基础验证,并提供凭据 auth: { }, // 服务器返回的数据类型 responseType: 'json', // 用作xsfr token的值的cookie的名称 xsrfCookieName: 'XSFR-TOKEN', // 承载xsfr token的值的HTTP头的名称 xsfrHeaderName: 'X-XSFR-TOKEN', // 处理上传进度事件 onUploadProgress: function(progressEvent){ }, // 处理下载进度事件 onDownloadProgress: function(){ }, // 允许的响应内容的最大尺寸 maxContentLength: 2000, // HTTP响应状态码 validateStatus: function(status){ }, // 最大重定向数目 maxRedirects: 5, // 执行http/https时使用的自定义代理 httpAgent: new http.Agent({keepAlive: true}), httpsAgent: new https.Agent({keepAlive: true}), //代理服务器的主机名称和端口 proxy: { host: '127.0.0.1', port: 9000, auth: { username: '', password: '' } }, // 指定用于请求的cancel token cancelToken: new CancelToken(function(cancel){ }) }
10.响应结构
使用then时axios.get('url').then(function(response){ console.log(response.data) // 服务器返回的数据 console.log(response.status) // 服务器返回的HTTP状态码 console.log(response.statusText) // 服务器返回的HTTP状态信息 console.log(response.headers) // 服务器的响应头 console.log(response.config) // 请求提供的配置信息 })
11.错误处理
axios.get('url').catch(function(error){ if(error.response){ console.log(error.response.data) console.log(error.response.status) console.log(error.response.headers) }else { console.log('Error', error.message) } console.log(error.config) })
相关文章推荐
- Vue 使用axios 发送post请求,参数格式有错误问题
- Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构
- "Android下使用HttpUrlConntion"进行最简单的get,post请求(包括创建服务器)
- Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- 使用jQuery的ajax方法向服务器发出get和post请求的方法
- 使用PHP开发一个简单的后台接口(响应移动端的get请求和post请求)
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- ajax和json的post和get请求参数配置
- php学习笔记(三十)ajax请求和接收参数的实现方式(包括json数据格式的简单处理)
- Ajax详解及其案例分析之如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表
- okhttp框架的最简单的基本使用 post和get请求以及用gson解析请求的数据
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- Retrofit简单封装使用--Post请求封装请求体、Get请求通过参数、map方式构建url
- 使用JMeter进行一次简单的带json数据的post请求测试,json可配置参数
- 表单中多个请求参数名字相同,服务器到底获取的是哪个请求参数的值,表单的action和get和post提交方式的关系以及提交数据的不同点
- vue-cli的axios使用方法,get、post请求的跨域问题解决
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- jQuery发送含有数组参数的ajax请求以及后台Struts2的OGNL解析错误