扫码关注官方订阅号
fetch('https://www.baidu.com', {mode: 'no-cors'}).then(res => res.text()).then(data => console.log(data))
network标签返回正常, 但是为什么输出的数据为空?
把这段代码有什么问题吗?
ringa_lee
指定 no-cors,浏览器当初跨域了 res的status已经是0了。
//try thisfetch('https://www.baidu.com', {mode: 'cors'}).then(res => res.text()).then(data => console.log(data))
修订
//try this fetch('https://cdn.bootcss.com/bootstrap/4.0.0-alpha.4/js/bootstrap.js', {mode: 'cors'}) .then(res => res.text()) .then(data => console.log(data))
因为百度的首页并未返回页面里头,并没有access-control-allow-origin的header,指定cors还是存在跨域,,会发生资源跨域问题。
cors 跨域值,当reponse满足cors时生效,参见HTTP访问控制(CORS)
no-cors 同域时候生效,默认值。
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
指定 no-cors,浏览器当初跨域了 res的status已经是0了。
//try thisfetch('https://www.baidu.com', {mode: 'cors'}).then(res => res.text()).then(data => console.log(data))修订
因为百度的首页并未返回页面里头,并没有access-control-allow-origin的header,指定cors还是存在跨域,
,会发生资源跨域问题。
cors
跨域值,当reponse满足cors时生效,参见HTTP访问控制(CORS)
no-cors
同域时候生效,默认值。