解決uni-app在H5端的跨域問題
阿新 • • 發佈:2021-01-01
問題報錯截圖:這是常見的前端瀏覽器跨域問題了
解決方法
修改 manifest.json
檔案,新增 H5
的跨域配置(使用 原始碼檢視開啟配置檔案)
// h5端跨域配置
"h5": {
"devServer": {
"disableHostCheck": true,
"proxy": {
"/api": {
"target": "http://xxxxxxxx",
"changeOrigin" : true,
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}
}
}
如何請求
把 H5
端的請求地址字首改為 /api
uni.request({
// #ifdef H5
url: '/api/postclass',
// #endif
// #ifdef APP-PLUS
url: this.config.webUrl + '/postclass',
// #endif
method: 'GET',
success: res => {
console.log(res)
},
fail: () => {},
});