問題描述:
項目中的企業微信內部應用,使用Vue-cli搭建的H5頁面web,在低版本的安卓手機或ios8.0、9.0中出現接口數據訪問失敗,HTTP狀態碼返回0的問題,無法正常使用系統。安卓手機主要出現問題得機型為oppo、vivo。
問題分析:
舊版本系統對ES6新特性不支持。比如箭頭函數,let,const。另外,還有少部分低版本安卓不支持小部分的ES5語法。
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,IE=IE9"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
在main.js中引入:require('es6-promise').polyfill()
或者
1 import Es6Promise from 'es6-promise' 2 Es6Promise.polyfill()
二:用於對ES6新語法不支持的瀏覽器
安裝依賴:npm install babel-polyfill -S
在main.js中引入:import "babel-polyfill",放在最頂部,確保全面加載
更改 webpack.base.conf.js 文件中的入口 entry:
1 entry: { 2 app: ["babel-polyfill", "./src/main.js"] //為低版本系統配置 3 //app: './src/main.js' //舊配置 4 },
重新打包發布,問題解決。
【補充拓展】IE瀏覽器中,以上方法設置后,依然會有報錯,還需要把es6轉es5

http中標准的head,認證名字叫做Authorization,A要大寫,但在vue用axios跨域請求時,傳的head名稱為authorization,是小寫,所以如果是使用IE瀏覽器,即使是設置Access-Control-Allow-Headers為*,也無法識別!
解決辦法:
后台服務器權設置Access-Control-Allow-Headers為'authorization,Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type',強制包含一個authorization頭即可
response.setHeader("Access-Control-Allow-Headers", "authorization,Authorization,DNT,X-CustomHeader," +"Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
或:

1 protected override void Application_BeginRequest(object sender, EventArgs e) 2 { 3 Response.Headers.Add("Access-Control-Allow-Origin", "*"); 4 if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")//攔截處理Options請求 5 { 6 string domain = Request.Headers.Get("Origin"); 7 8 Response.Headers.Add("Access-Control-Allow-Methods", "*"); 9 Response.Headers.Add("Access-Control-Allow-Headers", "authorization,Authorization,DNT,X-CustomHeader," + 10 "Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,*"); 11 Response.Flush(); 12 Response.End(); 13 } 14 base.Application_BeginRequest(sender, e); 15 }
注意:如果在后台接口層面進行了跨域設置,就不要在iis中在單獨設置http響應頭。