fetch设置mode='cors'无法跨域
我用fetch向后台请求一个数据,后台重定向到另外一个服务器去做权限验证,现在提示
No 'Access-Control-Allow-Origin' header is present on the requested resource.
改成mode='no-cors'之后,没有这个问题了,但是无法重定向到新的页面,返回的status=0.
有谁对fetch跨域了解的吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呵呵,我来说一句吧。
No 'xxx'表示你后台服务器没设cors,当然失败了。
至于你fetch设了mode='no-cors'表示不垮域,可以请求成功,但不好意思,你拿不到服务器返回数据,它被标记了'opaque',请求没发出去请看控制台网络面板,这我不多说
https://developer.mozilla.org...
设置了no-cors以后就不会发送跨域请求了,所以返回 status=0,因为请求根本没发出去.
正确的做法是
https://developer.mozilla.org...
让你们的服务器开发人员在Http Response 中添加Access-Control-Allow-Origin 头
这个页面有个CORS的实验,使用fetch,服务端是一个使用koa的不到10行的程序,你可以参考下,可能有帮助