No ‘Access-Control-Allow-Origin‘ header前端浏览器跨域用LiveServer处理
用Live Server解决静态页面跨域问题has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource。简而言之,不允许跨域。
·
1.报错
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
简而言之,不允许跨域
2. 代理处理
因为是纯前端代码,我们这里使用vscode插件Live Server代理并转发接口请求,从而绕过浏览器跨域问题,安装Live Server请点击这里。
-
打开配置Live Server
-
配置本地服务Host
默认host是127.0.0.1 -
配置本地服务端口
默认端口不改也没问题,如需修改点击【在 settings.json 中编辑】
按照实际需求配置端口号 -
设置代理,这一步才是代理跨域处理!!!
跨域enable的要设为true才能使用代理,然后识别需要代理的接口路径,并设置代理的地址。
在settings.json中也能看到配置的端口和代理 -
起本地服务,只要你路径设置正确了就可以代理解决浏览器跨域问题了
更多推荐
所有评论(0)