electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.47k stars 1.71k forks source link

使用electron-builder打包vue后无法调用接口 #6838

Open AdorableLi opened 2 years ago

AdorableLi commented 2 years ago

版本如下 "dependencies": { "axios": "^0.27.2", "core-js": "^3.8.3", "electron": "^18.2.0", "electron-builder": "^23.0.3", "path-browserify": "^1.0.1", "vue": "^2.6.14", "vue-router": "^3.5.2" },

在本地使用vue-router解决跨域后axios调用接口正常,但是打包后一直会catch回“AxiosError: Network Error”,并且控制台中报错为“Failed to load resource: net::ERR_FAILED”,无法调用接口,请问有人遇到过这个问题吗?

seanock commented 2 years ago

是不是跨域了呢?

AdorableLi commented 2 years ago

是不是跨域了呢?

本地解决了跨域,打包完之后应该不会存在跨域问题吧?难道是某些配置会影响到线上?

seanock commented 2 years ago

你本地是如何解决跨域的?

AdorableLi commented 2 years ago

你本地是如何解决跨域的?

配置vue-router还有代理啥的,然后后端去设置Access-Control-Allow-Origin啥的,本地是能够成功调用接口的

seanock commented 2 years ago

你可以这样设置一下,允许跨域,然后打包,如果还是不行就不是跨域问题 步骤一:按官方文档添加webSecurity项 webPreferences:{//网页功能的设置 nodeIntegration: true,//是否集成node webSecurity: false //是否禁用同源策略 }

步骤二:在文件中添加以下配置项 //解决10.X版本跨域不成功问题(上线删除) app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');

AdorableLi commented 2 years ago

你可以这样设置一下,允许跨域,然后打包,如果还是不行就不是跨域问题 步骤一:按官方文档添加webSecurity项 webPreferences:{//网页功能的设置 nodeIntegration: true,//是否集成node webSecurity: false //是否禁用同源策略 }

步骤二:在文件中添加以下配置项 //解决10.X版本跨域不成功问题(上线删除) app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');

第一个步骤我试过不行,我在其他地方看到过解释,因为9以上版本不在支持这样的配置了,只支持9以下的版本。

第二个步骤我今晚试一下。我是个前端所以不太了解接口,如果接口设置Access-Control-Allow-Origin为*的话,理论上来说应该是告诉浏览器允许所有的跨域请求嘛?

感谢大佬帮助🙏

seanock commented 2 years ago

别客气,咱俩同是小白,互帮互助。