【发布时间】:2018-05-23 22:53:49
【问题描述】:
查看新的 fetch API,您可以在请求中指定模式字段。来自Mozilla:
The mode read-only property of the Request interface contains the mode
of the request (e.g., cors, no-cors, same-origin, or navigate.) This is
used to determine if cross-origin requests lead to valid responses, and
which properties of the response are readable.
然后是如何使用它:
var myHeaders = new Headers();
var myInit = { method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default' };
fetch('flowers.jpg', myInit).then(function(response) {
return response.blob();
}).then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
我不明白您为什么需要具体说明在请求本身中如此不言自明的内容?如果我从客户端网站http://client.com 上的http://foo.com 服务器请求资源,服务器是否没有要查看的原始标头?这种模式不会引起混乱吗?
此外,我想弄清楚mode: same-origin 的目标是什么? “您可以使用它来确保始终向您的源发出请求。” 但是,如果您要发送请求,或者如果其他开发人员可以访问代码,为什么不这样做呢?你只是不发送请求而不是一个字段来表明它是无效的?
【问题讨论】:
标签: javascript xmlhttprequest cors frontend fetch-api