其他分享
首页 > 其他分享> > 手写ajax 请求并将获取到的图片二进制流渲染到页面上

手写ajax 请求并将获取到的图片二进制流渲染到页面上

作者:互联网

话不多说代码附上

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <img id="img" alt="">
  <script>
    const xhr = new XMLHttpRequest();

    xhr.open('GET', 'https://pic2.zhimg.com/50/v2-2038cd0997d58ff893d433761b90c102_hd.webp', true);
    xhr.responseType = "blob";
    
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          let res = xhr.response 
          console.log(res)
          img.src = window.URL.createObjectURL(res); 
        }
      }
    }

    xhr.send(null);
  </script>
</body>

</html>

xhr.readyState的值
readyState中的值的含义
status 返回状态吗含义
在这里插入图片描述

标签:status,readyState,XMLHttpRequest,二进制,res,v2,xhr,ajax,手写
来源: https://blog.csdn.net/weixin_43720534/article/details/114895451