axios
http://www.axios-js.com/docs/vue-axios.html 官网
伪代码
//const CancelToken = axios.CancelToken
// axios.defaults.withCredentials = false
import axios from 'axios'
import config from './config.js'
import { Loading } from 'element-ui'
const request = ()=>{
let loadingInstance; // 数据加载动画
const httpInstance = axios.create({
baseURL:'',
timeout: 30000
});
// 请求拦截
httpInstance.interceptors.request.use((config)=>{
if (options.loader) {
loadingInstance = Loading.service({
lock: true,
target: '.custom-view',
text: '数据加载中',
spinner: 'el-icon-loading',
background: 'rgba(255, 255, 255, 0.3)'
})
}
return config
},(error)=>{
loadingInstance && loadingInstance.close()
})
// 响应拦截
httpInstance.interceptors.response.use((response)=>{
loadingInstance && loadingInstance.close();
},(error)=>{
loadingInstance && loadingInstance.close();
})
return httpInstance(options)
}
export default request;
$.ajax()
- url 请求地址
- async 异步 默认为true
- cache 默认 true 如果当前请求有缓存的话,直接使用缓存
- type 请求类型 http 协议约定的数据请求类型 常用 post/get
- headers http 协议约定的报文头
- error 异常返回回调
- success 成功返回回调
- dataType 报文格式 xml/json/html/javascript/jsonp
$.ajax({
type: "post", // post/get
url: "",
data: {},
dataType: "json",
success(data){},
error(err){}
});