什么是不透明响应,它的用途是什么? [英] What is an opaque response, and what purpose does it serve?

查看:26
本文介绍了什么是不透明响应,它的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试fetch旧网站的网址,出现错误:

I tried to fetch the URL of an old website, and an error happened:

Fetch API cannot load http://xyz.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://abc' is therefore not allowed access.
If an opaque response serves your needs, set the request's mode to 'no-cors'
to fetch the resource with CORS disabled.

我理解了消息,并尝试执行返回不透明响应的请求:

I understood the message, and tried to do a request that returns an opaque response:

fetch("http://xyz", {'mode': 'no-cors'})

好的,它现在可以工作了……但我看不懂.=

Ok, it now works... but I can't read it. =

那么,不透明响应的目的是什么?

What's the purpose then, of an opaque response?

推荐答案

考虑 Service Worker 充当不可知缓存的情况.您唯一的目标是提供与您从网络获得的资源相同的资源,但速度更快.当然,您不能确保所有资源都属于您的来源(例如,考虑从 CDN 提供的库).由于 service worker 有可能改变网络响应,你需要保证你对响应的内容不感兴趣,也不关心它的头部,甚至不关心结果.您只对作为黑盒的响应感兴趣,以便可能缓存它并更快地提供它.

Consider the case in which a service worker acts as an agnostic cache. Your only goal is serve the same resources that you would get from the network, but faster. Of course you can't ensure all the resources will be part of your origin (consider libraries served from CDNs, for instance). As the service worker has the potential of altering network responses, you need to guarantee you are not interested in the contents of the response, nor on its headers, nor even on the result. You're only interested on the response as a black box to possibly cache it and serve it faster.

这就是 { mode: 'no-cors' } 的用途.

这篇关于什么是不透明响应,它的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆