前端跨域经验

8次阅读

共计 557 个字符,预计需要花费 2 分钟才能阅读完成。

接口跨域

jsonp

window.name + iframe

domain(主域相同)

cors

跨域资源共享 CORS 详解
重点: 1. 简单请求 & 复杂请求;2.cookie

图片跨域

canvas getImageData&toDataURL 也有跨域问题
解决方法:cors

a 标签 download 跨域失效

解决思路: 通过 fetch 或者 xhr 下载下来, 转为 blob, 再 createObjectURL
注意: 诺通过 axios 发请求下载文件出错, 具体原因未查

    const a = document.createElement('a');
    const url = "http://*******.zip";
    const filename = "文件名称";
    const xhr = new XMLHttpRequest();
    xhr.open('get', url);
    xhr.responseType = 'blob';
    xhr.onreadystatechange = function () {if (xhr.readyState === 4 && xhr.status === 200) {const blob = new Blob([xhr.response]);
        a.href = URL.createObjectURL(blob);
        a.download = filename;
        a.click();}
    };
    xhr.send();

正文完
 0