我已经研究过了,我的语法看起来是正确的,但我无法弄清楚出了什么问题。
如果我不包括fetch
options
参数,一切正常工作,除了CORS被打开了,这就是我不想要的。
在请求中添加options
和mode: 'no-cors'
后,现在我在所有请求的图像中都会收到这个错误:
image-cropper-modal.js:73 GET数据:net :: ERR_INVALID_URL
我已经尝试查看MDN 的使用 Fetch 和 https://jakearchibald.com/2015/thats-so-fetch/。
这是我代码:
const options = {
method: 'GET',
mode: 'no-cors'
};
fetch(url, options).then(response => response.blob())
.then(blob => {
let reader = new FileReader();
reader.onload = () => {
const type = blob.type;
const [image, dataUrl] = [new Image(), reader.result];
image.src = dataUrl;
const [width, height] = [image.width, image.height];
const {minWidth, minHeight} = props.opt;
const isWithinBounds = (width >= minWidth) && (height >= minHeight);
callback({
dataUrl,
height,
minHeight,
minWidth,
width
});
}
reader.readAsDataURL(blob)
});