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

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

问题描述

我试图获取一个旧网站的URL,并发生了一个错误:

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?

推荐答案

考虑服务工作者充当不可知缓存的情况。您唯一的目标是提供与网络相同的资源,但是速度更快。当然,您不能确保所有资源都属于您的来源(例如,考虑CDN提供的库)。由于服务人员有可能更改网络响应,因此您需要确保您对响应的内容,响应头,结果都不感兴趣。您只对作为黑匣子的响应感兴趣,可以将其缓存并更快地投放。

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.

这就是 {模式: -cors'} 的目的。

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

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