Flutter(十五)| Web本地测试跨域问题
Flutter的web在本地测试的时候,涉及到http跨域请求时,会报错:
Access to XMLHttpRequest at '***' from origin 'http://localhost:6715' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
可以把Flutter升级到3.3.0版本或以上版本,就可以使用下面的命令关闭CORS:
flutter run -d chrome --web-browser-flag "--disable-web-security"
那如何用局域网其他设备访问这个网站呢?例如PC(IP: 192.168.0.100)运行下面命令
flutter run -d chrome --web-browser-flag "--disable-web-security" --web-hostname 192.168.0.100 --web-port 13001
运行起来之后,可以在其他设备的浏览器中直接输入http://192.168.0.100:13001
就可以访问网站
如果你使用vscode调试,可以在launch.json中加入以下配置:
"version": "0.2.0",
"configurations": [
{
"name": "test",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"args": [
"-d",
"chrome",
"--web-renderer",
"html",
"--web-browser-flag",
"--disable-web-security"
]
}
]
0