• Fetch API & HTTP Response opaque All In One


    Fetch API & HTTP Response opaque All In One

    Promise {<rejected>: SyntaxError: Unexpected end of input at

      const promise = await fetch(url, {
        // method: 'GET', // *GET, POST, PUT, DELETE, etc.
        // mode: 'cors', // no-cors, *cors, same-origin
        // cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        // credentials: 'same-origin',
        // credentials: 'include',
        method: 'GET',
        // mode: 'no-cors',
        // ❌  A no-cors request makes the response type opaque. Opaque means your frontend JavaScript code can’t see the response body or headers.
        // responseType: 'arraybuffer',
        // responseType: 'blob',
        headers: {
          // 'Accept': type,
          'Content-Type': type,
          // 'Content-Type': 'application/octet-stream',
          // 'Accept': 'video/mp4',
          // 'Accept': 'application/json',
          // 'Content-Type': 'application/json;charset=utf-8'
          // 'Content-Type': 'application/json',
          // 'Content-Type': 'application/x-www-form-urlencoded',
          // ...headers,
        },
      }).then(res => {
        if(type.includes('json')) {
          const json = res.json();
          console.log('json =', json);
          return json;
        } else {
          const buffer = res.arrayBuffer();
          console.log('buffer =', buffer);
          return buffer;
        }
      }).then(data => {
        console.log('data =', data);
        const blob = new Blob(
          [data],
          {'type' : type},
        );
        const urlBlob = URL.createObjectURL(blob);
        dom.src = urlBlob;
        link.href = urlBlob;
        link.innerText = urlBlob;
      }).catch(err => {}).finally(() => {});
    
    

    https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input-when-using-mode-no-cors

    HTTP Response & type: 'opaque',

    // res = Response {type: 'opaque', url: '', redirected: false, status: 0, ok: false, …}body: (...)bodyUsed: falseheaders: Headers {}ok: falseredirected: falsestatus: 0statusText: ""type: "opaque"url: ""[[Prototype]]: Response
    fetch.js:48 buffer = Promise {<fulfilled>: ArrayBuffer(0)}
    
    

    ReadableStream

    type: "basic"

    url: "https://cdn.xgqfrms.xyz/HTML5/Blob/2022-04-07-sh.mp4

    // fetch.response = Response
    fetch.js:38 fetch.response = Response {type: 'basic', url: 'https://cdn.xgqfrms.xyz/HTML5/Blob/2022-04-07-sh.mp4', redirected: false, status: 200, ok: true, …}body: ReadableStreamlocked: true[[Prototype]]: ReadableStreamcancel: ƒ cancel()getReader: ƒ getReader()length: 0name: "getReader"arguments: (...)caller: (...)[[Prototype]]: ƒ ()[[Scopes]]: Scopes[0]locked: truepipeThrough: ƒ pipeThrough()pipeTo: ƒ pipeTo()tee: ƒ tee()constructor: ƒ ReadableStream()Symbol(Symbol.toStringTag): "ReadableStream"get locked: ƒ locked()[[Prototype]]: ObjectbodyUsed: trueheaders: Headers {}ok: trueredirected: falsestatus: 200statusText: ""type: "basic"url: "https://cdn.xgqfrms.xyz/HTML5/Blob/2022-04-07-sh.mp4"[[Prototype]]: ResponsearrayBuffer: ƒ arrayBuffer()blob: ƒ blob()body: ReadableStreambodyUsed: (...)clone: ƒ clone()formData: ƒ formData()headers: (...)json: ƒ json()ok: (...)redirected: (...)status: (...)statusText: ""text: ƒ text()type: "basic"url: "https://cdn.xgqfrms.xyz/HTML5/Blob/2022-04-07-sh.mp4"constructor: ƒ Response()Symbol(Symbol.toStringTag): "Response"get body: ƒ body()get bodyUsed: ƒ bodyUsed()get headers: ƒ headers()get ok: ƒ ok()get redirected: ƒ redirected()get status: ƒ status()get statusText: ƒ statusText()get type: ƒ type()get url: ƒ url()[[Prototype]]: Object
    
    

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

    原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!


  • 相关阅读:
    FZU 2113 BCD Code 数位dp
    Gym 100917L Liesbeth and the String 规律&&胡搞
    Gym 100917C Constant Ratio 数论+暴力
    CF149D Coloring Brackets
    P4342 [IOI1998]Polygon
    P4316 绿豆蛙的归宿
    P1439 【模板】最长公共子序列
    Noip 2013 真题练习
    洛谷比赛 「EZEC」 Round 4
    P5024 保卫王国
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/16133286.html
Copyright © 2020-2023  润新知