当前位置: 首页 > 科技观察

vue开发遇到使用axios的大坑!

时间:2023-03-14 14:26:28 科技观察

最近在做一个项目,项目需要一定的基础数据输入,所以在开发的时候让同事输入数据,结果遇到了sinkhole!我在局域网中使用angular的http请求连接开发项目,一切正常!使用vue的axios连接有的地方正常,有的地方不正常。服务器状态码为200,服务器端控制台不报错。但是页面请求是错误的。用iPhone手机报同样的错,换华为安卓手机报同样的错。不知道怎么调试安卓手机。使用macOS的Safari浏览器可以调试iPhone手机浏览器,调试报错,不知道是什么原因。只是报错,服务器没有问题。错误码是200,也就是说我的PC版浏览器放大到手机浏览器,一切正常,真机报错。如果是被ssl证书拦截,但实际上有的请求报错有的请求不报错。通常,服务器会返回某个错误。数据页会报错。切换到局域网内其他PC访问,很多页面正常,部分页面异步请求异常。所有的http请求都统一封装在一个axios工具中。为什么有的报错有的不报错?这太奇怪了。但是在我项目开发的本地机器上一切正常!例如加载区域数据时,数据为云南省行政区域的数据。我本机请求正常,其他浏览器访问局域网时,报网络请求错误,控制台显示编码不完整?但是编码是utf-8,与其他请求和返回完全一致。我的本机也可以正常加载。更离奇的是,打包成生产模式,部署到阿里云服务器上,一切正常。局域网跨机访问不正常,不是全部不正常,只是部分接口不正常。使用的代码是antdesignvuepro后台管理模板预置的axios包。代码如下:importVuefrom'vue'importaxiosfrom'axios'importstorefrom'@/store'importnotificationfrom'ant-design-vue/es/notification'import{VueAxios}from'./vueAxios'import{ACCESS_TOKEN,ENTERPRISE_TOKEN}from'@/store/mutation-types'importConstantfrom'@/config/ann.config'//创建axios实例constservice=axios.create({baseURL:Constant.API_URL,//apibase_urltimeout:6000//请求超时})consterr=(error)=>{if(error.response){constdata=error.response.dataconsttoken=Vue.ls.get(ACCESS_TOKEN)if(error.response.status===403){notification.error({message:'Forbidden',description:data.message})}if(error.response.status===401&&!(data.result&&data.result.isLogin)){notification.error({message:'Unauthorized',description:'Authorizationverificationfailed'})if(token){store.dispatch('Logout').then(()=>{setTimeout(()=>{window.location.reload()},1500)})}}}returnPromise.reject(error)}//requestinterceptorservice.interceptors.request.use(config=>{//登录用户认证Tokenconsttoken=Vue.ls.get(ACCESS_TOKEN)if(token){config.headers['Ann-Token']=token//让每个请求携带一个自定义的token,请按照修改实际情况自行决定}//当前运行的企业授权TokenconsttokenE=Vue.ls.get(ENTERPRISE_TOKEN)if(tokenE){config.headers['Ann-E-Token']=tokenE//让每个请求携带一个自定义的token请根据实际情况修改}returnconfig},err)//responseinterceptorservice.interceptors.response.use((response)=>{returnresponse.data},err)constinstaller={vm:{},install(Vue){看。use(VueAxios,service)}}export{installerasVueAxios,serviceasaxiosService}有人遇到过同样的问题吗?有遇到同样问题的朋友吗?