1、ionic serve调试时访问服务端数据。
解决跨域问题即可:在项目目录下的ionic.config.json里添加proxies字段
例如:
{ "name": "appv2", "app_id": "", "v2": true, "typescript": true, "proxies": [ { "path": "/ii", //ii为项目名 "proxyUrl": "http://localhost:8080/ii" } ] }
那么,当项目在请求“/ii/**”时,就会自动转为请求“http://localhost:8080/ii/**”
2、手机端APP访问服务器。
因为真机调试不是http://或者https://访问,而是file://,所以真机调试不需要跨域。
以访问本地服务器为例:手机和电脑在同一局域网下,请求时需要加上本地服务器地址,如“http://192.168.1.100:8080/ii/**”。
若访问不成功,可以尝试以下方法:
(1)安装白名单插件 ionic plugin add cordova-plugin-whitelist
在工程目录config.xml中添加:
<access origin="*"/> <allow-navigation href="*"/> <allow-navigation href="http://*/*"/> <allow-navigation href="https://*/*"/> <allow-navigation href="data:*"/> <allow-navigation href="tel:*"/> <allow-intent href="*"/> <access origin="*"/>
详情参考: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/
(2)防火墙问题。
以本地服务器为tomcat为例,在防火墙添加tomcat:控制面板——Windows防火墙——添加允许的应用——浏览——选择tomcat