如何修复“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”
Posted
技术标签:
【中文标题】如何修复“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”【英文标题】:How To Fix " No 'Access-Control-Allow-Origin' header is present on the requested resource." 【发布时间】:2018-04-12 15:10:30 【问题描述】:我正在尝试使用 YouTube 视频播放链接从另一台服务器获取数据,但出现此错误:
加载失败
https://r12---sn-aigllnd7.googlevideo.com/videoplayback?requiressl=yes&clen=2193105&signature=109DA57913EC4535C3AC8FB84356B9ABCD11D6C4.A8547FC1740F9913D3FEB1335605AF3477F2BA88&ipbits=0&mime=video%2Fmp4&initcwndbps=2508750&itag=18&ratebypass=yes&expire=1509471177&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&key=yt6&beids=%5B9466593%5D&gir=yes&id=o-AK9XTzJjMCSwyCVnb7md1uikHohpJqRX-MzC6VOT3BNa&mn=sn-aigllnd7&mm=31&pl=48&dur=28.560&lmt=1500337266737259&ip=2a02%3A4780%3Abad%3A16%3Afced%3A1ff%3Afe16%3A333&ms=au&ei=aV_4WabyCcTckAOJ_IHoBQ&mt=1509449472&mv=m&source=youtube:
从“https://r12---sn-aigllnd7.googlevideo.com/videoplayback?requiressl=yes&clen=2193105&signature=109DA57913EC4535C3AC8FB84356B9ABCD11D6C4.A8547FC1740F9913D3FEB1335605AF3477F2BA88&ipbits=0&mime=video%2Fmp4&initcwndbps=2508750&itag=18&ratebypass=yes&expire=1509471177&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&key=yt6&beids=%5B9466593%5D&gir=yes&id=o-AK9XTzJjMCSwyCVnb7md1uikHohpJqRX-MzC6VOT3BNa&mn=sn-aigllnd7&mm=31&pl=48&dur=28.560&lmt=1500337266737259&ip=2a02%3A4780%3Abad%3A16%3Afced%3A1ff%3Afe16%3A333&ms=au&ei=aV_4WabyCcTckAOJ_IHoBQ&mt=1509449472&mv=m&source=youtube”重定向到“https://r2---sn-ug5onfvgaq-aixl.googlevideo.com/videoplayback?requiressl=yes&clen=2193105&signature=2E5AA33529609CA727EB8BE12AA33FBDAAFA116F.02E023B012CCA899379E83DAC38CF55D9072907D&ipbits=0&mime=video%2Fmp4&itag=18&ratebypass=yes&expire=1509471177&sparams=clen,dur,ei,expire,gir,id,initcwndbps,ip,ipbits,ipbypass,itag,lmt,mime,mip,mm,mn,ms,mv,pl,ratebypass,requiressl,source&key=cms1&beids=%5B9466593%5D&gir=yes&id=o-AK9XTzJjMCSwyCVnb7md1uikHohpJqRX-MzC6VOT3BNa&pl=24&dur=28.560&lmt=1500337266737259&ip=2a02%3A4780%3Abad%3A16%3Afced%3A1ff%3Afe16%3A333&ei=aV_4WabyCcTckAOJ_IHoBQ&source=youtube&redirect_counter=1&rm=sn-aigeez7s&fexp=9466587&req_id=7a988fa941a7a3ee&cms_redirect=yes&ipbypass=yes&mip=210.56.10.190&mm=31&mn=sn-ug5onfvgaq-aixl&ms=au&mt=1509449472&mv=m”
已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost' 因此不允许访问。
你能告诉我如何解决这个错误。 感谢您宝贵的时间。
【问题讨论】:
除非您可以更改 googlevideo.com,否则您不会这样做。 【参考方案1】:在服务器端启用了CORS,因此您无法在代码中修复此问题。在某些情况下,您可以尝试使用 JSONP 的请求类型,但在这种情况下不行。
【讨论】:
以上是关于如何修复“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”的主要内容,如果未能解决你的问题,请参考以下文章