前端跨域经验

接口跨域

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();

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理