Chrome同源问题 不支持file协议

Chrome同源问题 不支持file协议

突然记起来。 想发一个博客。 答案摘自好几个博主。 具体记不清了

 $.ajax({
            url: "file:///Users/hao.yan/Downloads/4-20/%E5%95%86%E5%93%81%E5%A4%87%E6%A1%88list.html",
            method: "get",
            async: false,
            head: {'Content-Type': 'application/x-www-form-urlencoded'},
            success: function (data) {
                var destCountryNameC = '';
                
                
 

上述代码是通过ajax直接去获取本地的html文件。但是在获取的过程中 会报错Access-control-Allow-Origin

m origin 'file://' 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. 

这是因为浏览器(Webkit内核)的安全策略决定了file协议访问的应用无法使用XMLHttpRequest对象。 错误消息也狠清楚的说明了

Cross origin requests are only supported for protocol schemas: http data chrome chrome-extension https chrome-extension-resource 
跨域请求仅支持协议。http data chrome chrome-extension https chrome-extension-resource

如何解决?

不同的系统解决方式不同

Windows系统

Chrome49以后的版本

 --disable-web-security --user-data-dir=<用户数据路径> --allow-file-access-from-files
 完整示例  
 C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe --disable-web-security --user-data-dir=C:\MyChromeDevUserData --allow-file-access-from-files


直接在chrome快捷方式后加参数即可

chrome49之前的版本

--disable-web-security  直接在chrome快捷方式后加这个参数即可

Mac系统 关闭浏览器。直接在终端输入即可。

Chrome49之后的版本

open -a <APP路径> --args --disable-web-security --user-data-dir=<用户数据路径>

完整示例。
open -a /Applications/Google\ Chrome.app --args --disable-web-security --user-data-dir=/Users/mac/Documents/MyChromeDev

chrome49之前的版本

open -a "Google Chrome" --args --disable-web-security

//Safari浏览器
open -a '/Applications/Safari.app' --args --disable-web-security
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值